问题描述
对于一个项目,有人给了我我在Postman中用于测试目的的数据:
For a project someone gave me this data that I have used in Postman for testing purposes:
在邮递员中,这很完美.
In Postman this works perfectly.
验证URL: https://api.example.com/oauth/access_token
访问令牌URL: https://api.example.com/access_token
客户端ID:abcde
客户机密:12345
令牌名称:access_token
授予类型:客户凭证
Auth URL: https://api.example.com/oauth/access_token
Access Token URL: https://api.example.com/access_token
client ID: abcde
client secret: 12345
Token name: access_token
Grant type: Client Credentials
我需要做的就是取回访问令牌.
All I need is to get back the access token.
一旦获得访问令牌,我就可以继续.
Once, I got the access token I can continue.
我已经尝试了几个Python软件包和一些自定义代码,但是以某种方式看似简单的任务却开始令人头疼.
I have already tried several Python packages and some custom code, but somehow this seemingly simple task starts to create a real headache.
我尝试过的一个例子:
import httplib
import base64
import urllib
import json
def getAuthToken():
CLIENT_ID = "abcde"
CLIENT_SECRET = "12345"
TOKEN_URL = "https://api.example.com/oauth/access_token"
conn = httplib.HTTPSConnection("api.example.com")
url = "/oauth/access_token"
params = {
"grant_type": "client_credentials"
}
client = CLIENT_ID
client_secret = CLIENT_SECRET
authString = base64.encodestring('%s:%s' % (client, client_secret)).replace('\n', '')
requestUrl = url + "?" + urllib.urlencode(params)
headersMap = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + authString
}
conn.request("POST", requestUrl, headers=headersMap)
response = conn.getresponse()
if response.status == 200:
data = response.read()
result = json.loads(data)
return result["access_token"]
然后我得到了这个:
import requests
import requests.auth
CLIENT_ID = "abcde"
CLIENT_SECRET = "12345"
TOKEN_URL = "https://api.example.com/oauth/access_token"
REDIRECT_URI = "https://www.getpostman.com/oauth2/callback"
def get_token(code):
client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)
post_data = {"grant_type": "client_credentials",
"code": code,
"redirect_uri": REDIRECT_URI}
response = requests.post(TOKEN_URL,
auth=client_auth,
data=post_data)
token_json = response.json()
return token_json["access_token"]
如果这行得通,我应该在 code
参数中放入什么
If this would work, what should I put into the code
parameter
我真的希望有人能在这里帮助我.
I really hope someone can help me out here.
谢谢.
推荐答案
我终于能够完成它
这是我使用的代码:
class ExampleOAuth2Client:
def __init__(self, client_id, client_secret):
self.access_token = None
self.service = OAuth2Service(
name="foo",
client_id=client_id,
client_secret=client_secret,
access_token_url="http://api.example.com/oauth/access_token",
authorize_url="http://api.example.com/oauth/access_token",
base_url="http://api.example.com/",
)
self.get_access_token()
def get_access_token(self):
data = {'code': 'bar',
'grant_type': 'client_credentials',
'redirect_uri': 'http://example.com/'}
session = self.service.get_auth_session(data=data, decoder=json.loads)
self.access_token = session.access_token
这篇关于如何使用Python获取oauth2 access_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!