问题描述
我正在尝试从 Sabre Dev Studio 获取身份验证令牌.我正在遵循此处给出的通用说明 https://developer.sabre.com/docs/read/rest_basics/authentication(需要登录才能查看)但我不知道如何使用 Python 获取令牌 - 特别是使用 python-oauth2 库,因为它似乎被推荐用于简化过程.
I am trying to get an authentication token from the Sabre Dev Studio. I am following the generic directions given here https://developer.sabre.com/docs/read/rest_basics/authentication(need to login to view) but I cannot figure out how to obtain a token using Python - specifically using the python-oauth2 library as it seems to be recommended to simplify the process.
这是我的代码示例:
config = ConfigParser.ConfigParser()
config.read("conf.ini")
clientID = config.get('DEV', 'Key').strip()
clientSecret = config.get('DEV', 'SharedSecret').strip()
consumer = oauth.Consumer(key=base64.b64encode(clientID),
secret=base64.b64encode(clientSecret))
# Request token URL for Sabre.
request_token_url = "https://api.sabre.com/v1/auth/token"
# Create our client.
client = oauth.Client(consumer)
# create headers as per Sabre Dev Guidelines https://developer.sabre.com/docs/read/rest_basics/authentication
headers = {'Content-Type':'application/x-www-form-urlencoded'}
params = {'grant_type':'client_credentials'}
# The OAuth Client request works just like httplib2 for the most part.
resp, content = client.request(request_token_url, "POST", headers=headers)
print resp
print content
响应类型为 401.其中凭据不完整或格式错误.任何建议表示赞赏.
The response is a type 401. Where the credentials are incomplete or misformed.Any suggestions appreciated.
推荐答案
首先获取您的凭据:
- 在 https://developer.sabre.com/member/register 中注册立>
- 登录https://developer.sabre.com
转到https://developer.sabre.com/apps/mykeys 并获取您的凭据.它们应该看起来像这样(垃圾邮件和鸡蛋看起来像垃圾):
- Register in https://developer.sabre.com/member/register
- Sign in into https://developer.sabre.com
Go to https://developer.sabre.com/apps/mykeys and get your credentials. They should look like this (where spam and eggs will look like garbage):
client_id = 'V1:spam:DEVCENTER:EXT'
client_secret = 'eggs'
然后调用 /v2/auth/token
端点以获取访问令牌:
Then call the /v2/auth/token
endpoint in order to get an access token:
import requests
credentials = ":".join([part.encode('base64').strip()
for part in (client_id, client_secret)]
).encode('base64').strip()
url = 'https://api.test.sabre.com/v2/auth/token'
headers = {'Authorization': 'Basic ' + credentials}
params = {'grant_type': 'client_credentials'}
r = requests.post(url, headers=headers, data=params)
assert r.status_code is 200, 'Oops...'
token = r.json()
print(token)
你应该得到这样的东西:
You should get something like this:
{u'access_token': u'T1RLAQJwPBoAuz...x8zEJg**',
u'token_type': u'bearer',
u'expires_in': 604800}
现在您可以使用包含您的访问令牌的 Authorization
标头调用其他端点.例如,如果您想要支持的国家/地区列表:
Now you can call others endpoints using an Authorization
header containing your access token. For example, if you want the list of supported countries:
headers = {'Authorization': 'Bearer ' + token[u'access_token']}
endpoint = 'https://api.test.sabre.com/v1/lists/supported/countries'
r = requests.get(endpoint, headers=headers)
assert r.status_code is 200, 'Oops...'
print (r.json())
如果一切顺利,您应该会收到支持的国家/地区列表:
If everything went well, you should receive the list of supported countries:
{u'DestinationCountries': [
{u'CountryName': u'Antigua And Barbuda', u'CountryCode': u'AG'},
{u'CountryName': u'Argentina', u'CountryCode': u'AR'},
{u'CountryName': u'Armenia', u'CountryCode': u'AM'},
{u'CountryName': u'Aruba', u'CountryCode': u'AW'},
...
}
这篇关于如何使用 python 为 Sabre Dev Network 执行 oauth2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!