Forum

Browse topics, discover Works With Legrand community!

Again on Oauth2: python

Hello,

I am struggling in getting the oauth2 working on python. I created an Application on Legrand, so I have the ID and Secrets. Because App, I do not have a redirection uri.

According to the tutorial, using a get request I can complete the Step1. The request returns “OK 200”, but I cannot find the Code. From I can see it seems that the request return contains a sort of webpage (or the code of an HTML page); what does it means? I mean after the completition of the request I was supposing to have the code, according to your tutorial.

Please give me some more directions. Thank you very much for your support.

Lorenzo

Thank you! I got it now.

 

Lorenzo

Hi Leslie,

it is me again. I have some more question, but I do not know if we are in the right section of the forum.

Now I have my token, I have the refresh token. The refresh flow is working as well since I am receiving message 200OK.

Next step is to retrive my plantId by means of the proper API using into the header the token and the subscription key and a get request.

I am receiving this message:

{ “statusCode”: 404, “message”: “Resource not found” }

What does it mean? Thank you again for your support.

Lorenzo

Hi Lorenzo,

There is a simple way, via the Try It, to retrieve your plantId (as it doesn’t change, you can just store it) :

You will retrieve a JSON with the information

Have a good day,

Leslie – Community Manager

Hi Leslie,
something strange. Using the Try button I can retrive the plantID, but using the get from Python I am receiving the error 404.

Any idea?

Lorenzo

Lorenzo,

I’m not a Python expert so it will be hard for me to help you with code

But we have a Python example for this call at the bottom of this page : https://portal.developer.legrand.com/docs/services/59e9ad7f8bda9e0b3caea28c/operations/getusersplants

Did you try with it ?

I also did a request via Postman and here is the generated Python code snippet :

import requests

url = “https://api.developer.legrand.com/hc/api/v1.0/plants”
payload = {}
headers = {
‘Ocp-Apim-Subscription-Key’: ‘9a44c88bfaa34XXXXXXXX’,
‘Authorization’: ‘Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ilg1ZVhrNHh5b2pORnVtMWtsMll0djXXXX’
}

response = requests.request(“GET”, url, headers=headers, data = payload)

print(response.text.encode(‘utf8’))

 

Hoping it could help you,

Have a good day,

Leslie – Community Manager

OK I will try later. What I can observe is that the token I have is not starting with string “Bearer ” but it is starting with the long strings of letters and numbers. Could it be the root cause?

Lorenzo

I’ll let you try

Maybe the error message for this issue is not the good one (like the 500 error you received a few days ago). Normally, you should receive a 4xx error and a sentence to tell you that there is a problem with the token

Have a good day,

Leslie – Community Manager

Hello Leslie,

about my issue I did some further investigation. The error I am receiving is “Unauthorized. Supplied access token is invalid or doesn\’t include required scope(s).”}'”.

I tried with the “Try” button and it works, I tried with my app with no success. I did another test using the token generated with the Try button into my application, with no success.

I read another user had a identical issue months ago (Giancarlo is the name), he solved creating a new app.

Please let me know.

Lorenzo

Hello Lorenzo,

It’s better not to use the “Try it” token as it’s provided to get a simplified way to test the API and it can’t be refreshed

I remember the case of Giancarlo, can you please send me an email through the contact form with your application ID and the scopes you asked ? I’ll be able then to check if something is misconfigured

Thanks and have a good day,

Leslie – Community Manager

Hi Leslie, thank you again for your help. I do not know the reason why but right now I am able to get plant ID and plant topology.

Now I am facing another issue… 🙂 I am trying to get the status of a light and I am trying to set the status of a light. I am receiving this error when reading the status

{“errors”:[{“userMessage”:””,”internalMessage”:”At least one of these scopes missing: light.read”,”code”:303}]}

and this error when I am writing the status:

{“errors”:[{“userMessage”:””,”internalMessage”:”At least one of these scopes missing: light.write”,”code”:303}]}

Could you help me again?

Thank you!

Lorenzo

 

Hi Lorenzo,

It simply means that … light scopes are missing 😀

Either you don’t ask for those scopes in your application, or it’s misconfigured on our side

Please verify in your application’s detail if light.read and light.write scopes are checked. If no, check them and send your application for approval. If those boxes are already checked, please send me your application_id (client_id) via the contact form and I’ll manually add them

Have a good day,

Leslie – Community Manager

Hello Leslie, I created a new application according to you. Using the new application I can “control” my devices. Thank you very much for your support.

 

Lorenzo

Hi Lorenzo,

Happy to read that !

Have a good day,

Leslie – Community Manager

Hi,

I’m currently trying something similar but on a PinePhone (I would like to get rid of Android but still have Home+Control), using a python program, so I thought it was best to post my request here ;).

I managed to get the code, but I’m blocked at getting the token. I’ve tried both in python and with curl but I always get the same 500 error (unexpected server error). I’ve already managed to perform echo with curl and python so I assume I’m doing ok with my request …

Here is my curl command line, maybe someone can spot what I’m doing wrong ?

First tried passing parameters in the header

curl -v -X POST “https://partners-login.eliotbylegrand.com/token” \
-H “Content-Type: application/json” \
-H “client_id: b8b6XXXX” \
-H “client_secret: _MXXX” \
-H “grant_type: authorization_code” \
-H “code: eyJraXXX” -d “”

then in the data

curl -v -X POST “https://partners-login.eliotbylegrand.com/token” \
-H “Content-Type: application/json” \
-d ‘”client_id” : “b8b6XXX”,
“client_secret” : “_M9yXXX”,
“grant_type” : “authorization_code”,
“code” : “eyJraXXX”‘

Thank you for the support,

Regards

FdL

I think I got it working, by passing data as forms (-F) instead of in the header (-H), also had to remove the content-type …

Had I seen topic by amaury, I would not have posted here …

Regards

FdL

Viewing 15 posts - 16 through 30 (of 36 total)

You must be logged in to reply to this topic.

Information

35 replies
5 participants