Home > database >  How to get token in Python 0AthLib OLX Partner API V2
How to get token in Python 0AthLib OLX Partner API V2

Time:10-22

I study API OLX https://developer.olx.ua/api/doc. I'm trying to implement authorization and getting a token in Python. Stuck at the moment of receiving the Authorization Code. I am a beginner, I have never worked with this type of authorization.

Here is my piece of code

client_id = 'xxxx'
client_secret = 'xxxx'
authorization_base_url = 'https://www.olx.ua/oauth/authorize'
token_url = 'https://www.olx.ua/api/open/oauth/token'
redirect_uri = 'http://www.example.com/'     # Should match Site URL
scope = 'v2 write read'
grant_type='authorization_code'
import hashlib
import requests
from authlib.integrations.requests_client import OAuth2Session
olx = OAuth2Session(client_id, client_secret, scope=scope,redirect_uri=redirect_uri)
authorization_uri, state = olx.create_authorization_url(authorization_base_url)
user_agent_val = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
   (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
payload={'username': '[email protected]','password': hashlib.md5(b"xxxxx")}  
headers = {'User-Agent':user_agent_val}
response = requests.request("GET", authorization_uri, headers=headers, data=payload)
print(response.url)  

I expect to receive in a variable response a string of the redirect_url/code&state, but I get string authorization_uri If I enter line authorization_uri into the browser after authorization, I am redirected to the required redirect_url/code&state What am I doing wrong?

CodePudding user response:

There are two things here. You first need to construct Authorize url with expected parameters like redirect_uri, grant_type,client_id, client_secret and state. state here would be some random string that is an identifier for your state. Some auth providers expect PKCE strings.

For eg. Auth Url can be https://www.olx.ua/oauth/authorize/?client_id=123&response_type=code&state=x93ld3v&scope=read write v2&redirect_uri=http://myadomain.com/auth/connect (Note: this will be a get call, you must redirect to this url )

where you parameters are:

{
"client_id": 123,
"response_type": code,
"state": x93ld3v,
"scope": "read write v2",
"redirect_uri': "http://myadomain.com/auth/connect"
}

You also need to add in a logic in backend server or UI whichever is server in the redirect_uri to handle params posted by olx server to make further calls for auth token.

Once the server authenticates, it will redirect to the redirect_uri with code and state. You can match this state with your state to ensure that this is the correct code. Code is your authorizer for current session which will be required in next step.

You can then use this code again to make a POST call over olx api to retrieve auth token ( endpoint: /api/open/oauth/token).

{
  "grant_type": "authorization_code",
  "client_id": "",
  "client_secret": "",
  "code": "",
  "scope": "v2 read write",
  "redirect_uri": "http://myadomain.com/auth/connect"
} 

You must receive an access token and a refresh token which you can use as per your needs.

You can refer olx.ua Auth documentation for more details.

CodePudding user response:

authorization_uri, state = olx.create_authorization_url(authorization_base_url)

in authorization_uri, i get string https://www.olx.ua/oauth/authorize?response_type=code&client_id=12345&redirect_uri=http://www.example.com/ru/&scope=v2 write read&state=6yDKvzjFfN0uXrGJMUUUgjz3pzNilB

which I send response = requests.request("GET", authorization_uri, headers=headers, data=payload)

How to correctly send a request to get redirect URL with code

  • Related