getToken() error while connecting to TigerGragh locally using pyTigerGraph - LocationParseError: Failed to parse: http://localhost:14240:9000/requesttoken?secret=xxx

Greetings:
I am new to TigerGraph. I have it installed as a docker image on my windows laptop and I can get into GraphStudio, GSQL prompt etc. I have installed pyTigerGraph and while trying to get a Token, I am getting the following error. Please advise.

Connection parameters

hostName = “http://localhost:14240
secret = conn.createSecret()
#token = conn.getToken(secret=secret, setToken=True, lifetime=None)[0]
token = conn.getToken(secret=secret)[0]

LocationParseError Traceback (most recent call last)
~\anaconda3\lib\site-packages\requests\models.py in prepare_url(self, url, params)
379 try:
→ 380 scheme, auth, host, port, path, query, fragment = parse_url(url)
381 except LocationParseError as e:

~\anaconda3\lib\site-packages\urllib3\util\url.py in parse_url(url)
391 except (ValueError, AttributeError):
→ 392 return six.raise_from(LocationParseError(source_url), None)
393

~\anaconda3\lib\site-packages\urllib3\packages\six.py in raise_from(value, from_value)

LocationParseError: Failed to parse: http://localhost:14240:9000/requesttoken?secret=xxx

During handling of the above exception, another exception occurred:

InvalidURL Traceback (most recent call last)
in
5
6 #token = conn.getToken(secret=secret, setToken=True, lifetime=None)[0]
----> 7 token = conn.getToken(secret=secret)[0]

~\anaconda3\lib\site-packages\pyTigerGraph\pyTigerGraph.py in getToken(self, secret, setToken, lifetime)
1378 “&lifetime=” + str(lifetime) if lifetime else “”)).text)
1379 else:
→ 1380 res = json.loads(requests.request(“GET”, self.restppUrl + “/requesttoken?secret=” + secret + (
1381 “&lifetime=” + str(lifetime) if lifetime else “”),verify=False).text)
1382 if not res[“error”]:

~\anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
59 # cases, and look like a memory leak in others.
60 with sessions.Session() as session:
—> 61 return session.request(method=method, url=url, **kwargs)
62
63

~\anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
514 hooks=hooks,
515 )
→ 516 prep = self.prepare_request(req)
517
518 proxies = proxies or {}

~\anaconda3\lib\site-packages\requests\sessions.py in prepare_request(self, request)
447
448 p = PreparedRequest()
→ 449 p.prepare(
450 method=request.method.upper(),
451 url=request.url,

~\anaconda3\lib\site-packages\requests\models.py in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json)
312
313 self.prepare_method(method)
→ 314 self.prepare_url(url, params)
315 self.prepare_headers(headers)
316 self.prepare_cookies(cookies)

~\anaconda3\lib\site-packages\requests\models.py in prepare_url(self, url, params)
380 scheme, auth, host, port, path, query, fragment = parse_url(url)
381 except LocationParseError as e:
→ 382 raise InvalidURL(*e.args)
383
384 if not scheme:

InvalidURL: Failed to parse: http://localhost:14240:9000/requesttoken?secret=xxx

@Prads

This shows it’s passing 14240 and 9000 ports. Try to remove ports from the host name. pyTigerGraph will automatically pass port information

hostName="127.0.0.1"

or

hostName = "http://localhost"

Thanks for the response Jon. I really appreciate the help.

I tried both as you suggested and still get the same error. I am not passing 9000 host name anywhere and somehow the hostName is getting padded with : 9000 -
InvalidURL: Failed to parse: http://localhost:14240:9000/requesttoken?secret=

Hi Prads,

I was able to recreate this issue and also get token creation working in my environment. A few things are going on:

First, it looks like you are using a local docker container (yes?), and so the pyTigerGraph class is trying to set hostname by adding the default RESTPP port (9000) not 14240 which is for Studio.

Next, have you enabled RESTPP Authentication within the docker container? The docker image ships with RESTPP disabled, instructions to enable can be found here: (https://docs.tigergraph.com/tigergraph-server/current/user-access/enabling-user-authentication)

Lastly and most importantly, I notice you don’t show the actual connection create statement? The statement also needs to specify a Graph, so you need to add the graphname = ‘MyGraph’ parameter.

I was able to connect a return a token using the following statement:

conn = tg.TigerGraphConnection(
host=‘http://localhost’,
graphname = ‘MyGraph’,
username=‘tigergraph’,
password=‘tigergraph’)

secret = conn.createSecret()
token = conn.getToken(secret)
print(token)

(‘p35l2p9t1i6itsj7emdjsuwysoo42sr6huth’, 1645586238, ‘2022-02-23 03:17:18’)

I do think there is a bug in pyTigerGraph where host doesn’t work (you get 14240:9000) unless you are using the docker container with the 14240 & 9000 ports forwarded. That way simple ‘localhost’ gets handled correctly.

1 Like

Hi rhardaway,
That worked. Thank you very much for your help and trying it by yourself to recreate the issue and finding the fix. I truly appreciate it.

Thanks again

1 Like

No problem Prads, I struggled with the issue myself. I’m planning to add this to a blog and possibly our documentation

1 Like

I am having issues with gsql command while using the cloud instance. I was able to connect to the cloud instance using pyTigerGraph, get a secret, token etc. conn.echo() returned ‘Hello GSQL’. However, when I tried a gsql command, I got the following error.
Again, I am using a windows laptop, jupyter notebook and pyTigerGraph.

I tried installing gsql_client using pip install as well as using the .jar file. .jar file install is looking for an ip address and the install is not successful.

Thanks in advance.

print(conn.echo())
print()
    
conn.gsql('ls')

Hello GSQL


FileNotFoundError                         Traceback (most recent call last)
~\anaconda3\lib\site-packages\pyTigerGraph\pyTigerGraph.py in initGsql(self, certLocation)
   1637             try:
-> 1638                 certcontent = open(self.certLocation, 'w')
   1639                 certcontent.write(Res)

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\prad_/.gsql/my-cert.txt'

During handling of the above exception, another exception occurred:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-53-cae666d9c47f> in <module>
      2 print()
      3 
----> 4 conn.gsql('ls')

~\anaconda3\lib\site-packages\pyTigerGraph\pyTigerGraph.py in gsql(self, query, graphname, options)
   1682             graphname = ""
   1683         if not self.gsqlInitiated:
-> 1684             self.gsqlInitiated = self.initGsql()
   1685         if self.gsqlInitiated:
   1686             if "\n" not in query:

~\anaconda3\lib\site-packages\pyTigerGraph\pyTigerGraph.py in initGsql(self, certLocation)
   1643                 self.certLocation = "/tmp/my-cert.txt"
   1644 
-> 1645                 certcontent = open(self.certLocation, 'w')
   1646                 certcontent.write(Res)
   1647                 certcontent.close()

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/my-cert.txt'
1 Like