简体   繁体   中英

Why I am receiving 405 error for request?

I am trying to send a post request using python3.6 to oauth2 server to get my access token, it is showing response 405. Can someone tell me what I am doing wrong here

import requests
client_id = 'client_id'
client_secret = 'my client secret'
r = request.post('serverurl/oauth2/token&Content-Type="application/x-www-form-urlencoded"',data = {'grant_type':'authorization_code',
'code':'91a8a5e4-c5b3-4e2a-91ca-d59fe139526c',
'client_id':client_id,
'client_secret':client_secret,
'redirect_uri':'actualredirecturl'
}

I solved it using the following code

here is the code

import requests,base64
client_id = "myclientid"
client_secret = "myclientsecret"
redirect_uri = 'my redirect url'
code = 'code return by server on login'
requestHeaders = {
    'Authorization': 'Basic ' + base64.b64encode(client_id + ':' + client_secret),
    'Content-Type': 'application/x-www-form-urlencoded'
}
requestBody = {
    'grant_type': 'authorization_code',
    'code': code,
    'redirect_uri': redirect_uri
}
response = requests.post('token_url', data=requestBody, headers=requestHeaders)
token = response.json()['access_token']

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM