API Token Error
in Development
I am trying to use a python script to get a token.
This is the error I receive.
{"error":"invalidgrant","errordescription":"Authorization code doesn't exist or is invalid for the client"}
Python script
import requests
CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CODE = "Temporary Token"
RTYPE = "POST"
GTYPE = "authorization_code"
url = "https://cloud.lightspeedapp.com/oauth/access_token.php"
payload = {
"client_id": {CLIENT_ID},
"client_secret" : {CLIENT_SECRET},
"code": {CODE},
"grant_type": GTYPE}
print(payload)
response = requests.request(RTYPE, url, data=payload)
print(response.text)
Answers
Hey @marydeangelo,
Great question! I think I've identified the problem in your code that can easily fixed! :)
The payload in the request method needs to be stringified like so
response = requests.request(url, data=json.dumps(payload, headers=headers)
You can do that using the built in python json module. Make sure to import it and add it to your requirements.txt file and your main file as shown below.
Also you want to specify the content-type of the data you're sending:
So your code should look like this:
Here is an image below of how I write the code. This works for me :)
Hope this helps!
Adrian Samuel
Software Developer
Lightspeed HQ
import requests
import json
CLIENT_ID = "xxxx"
CLIENT_SECRET = "zzzzzz"
CODE = "Temporary Token"
RTYPE = "POST"
GTYPE = "authorization_code"
url = "https://cloud.lightspeedapp.com/oauth/access_token.php"
payload = {
'client_id' : {CLIENT_ID},
'client_secret' : {CLIENT_SECRET},
'code' : {CODE},
'grant_type' : {GTYPE}
}
headers = {'content-type' : 'application/json'}
r = requests.post(RTYPE, url, data=json.dumps(payload),headers=headers)
print(response.text)
I tried the following and got these errors
C:\pythonmmd\lightspeed>py t13.py
Traceback (most recent call last):
File "t13.py", line 19, in <module>
r = requests.post(RTYPE, url, data=json.dumps(payload),headers=headers)
File "C:\Users\mary.deangelo\Anaconda3\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Users\mary.deangelo\Anaconda3\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\mary.deangelo\Anaconda3\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\mary.deangelo\Anaconda3\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'set' is not JSON serializable
Hey @marydeangelo, if you specify request.post, you don't need to put the request type afterwards as a first parameter since its defined on the class.
So you can write it as:
r = requests.post url, data=json.dumps(payload),headers=headers)
OR
response = requests.request(RTYPE,url, data=json.dumps(payload, headers=headers)
Adrian Samuel
Software Developer
Lightspeed HQ
I tried the following
headers = {'content-type' : 'application/json'}
response = requests.request(RTYPE,url, data=json.dumps(payload, headers=headers)
print(response.text)
I got the error
print(response.text)
^
SyntaxError: invalid syntax
@marydeangelo,
I can't diagnose syntax errors in rich text since it could simply be due to copy and pasting from this forum, but any syntax should be easily caught with a recommend fix from a good linter.
If you copy the example I've listed in the screenshot in my response exactly, it will work
Adrian Samuel
Software Developer
Lightspeed HQ
I used the python code found on this link
with r = requests.post
I get the following error.
[('error', 'invalidrequest'), ('errordescription', 'The request method must be POST when requesting an access token'), ('error_uri', 'http://tools.ietf.org/html/rfc6749#section-3.2')]
with r=request.get I get this error
C:\pythonmmd\lightspeed>py aaa4.py
[('error', 'invalid_client'), ('error_description', 'The client credentials are invalid')]
I have also tried this python script and the results is a 400 error.
See attached
Hey @marydeangelo,
It looks like the python snippet on the documentation is incorrect or potentially is an old implementation of the access_token endpoint before my time.
Here's a working copy on how to get the access token on a live python repl environment:
First: Insert your: client_id, client_secret and temporary code when you user-authenticate,
Second: Run the file with the green run button at the top and it will log your credentials to the virtual terminal and create a file
Hope this helps!
Adrian Samuel
Software Developer
Lightspeed HQ
what would my temporary code be?
@marydeangelo,
Your temporary code is the code that is generated when you authorise the application.
To get that, put this URL in your address bar:
https://cloud.lightspeedapp.com/oauth/authorize.php?responsetype=code&clientid={client_id}&scope=employee:all
Make sure to fill in your {clientid} part with your actual client id
Then you will arrive at a Lightspeed login screen. From there you will put your username and password for the account to login. It will take you to a screen where you will be able to authorise the application. Press the black button to authorise and then it will take you to your redirect URI and the temporary code (which lasts for 30 seconds) will be appended as a URL parameter.
You'll be able to read details about this here:
https://developers.lightspeedhq.com/retail/authentication/authentication-overview/#requesting-a-temporary-token
Adrian Samuel
Software Developer
Lightspeed HQ
I don't have a login for Lightspeed login screen
@marydeangelo, that will be a requirement.
If you have an application someone has to trust and enable your application on their account.
To do this they give the "okay" by authorising your app on their account. This requires credentials or anybody could add/remove/change data on anybody's account.
If you work for a business, you can ask someone to authorise the application for you, or it might be easier to get them to add you as an employee.
Alternatively, you can create a free retail account trial and test out the authentication workflow there.
You can create a free retail account trial with the following link:
https://www.lightspeedhq.com/trial/
Adrian Samuel
Software Developer
Lightspeed HQ