How do I perform oauth2 for Sabre Dev Network using python?

◇◆丶佛笑我妖孽 提交于 2019-12-05 20:27:02

I could not with oauth2, but I did with requests package I think you can get it from here

My code is:

import requests
import base64
import json


def encodeBase64(stringToEncode):
    retorno = ""
    retorno = base64.b64encode(stringToEncode)    
    return retorno


parameters = {"user": "YOUR_USER", "group": "YOUR_GROUP", "domain": "YOUR_DOMAIN", "password": "YOUR_PASSWORD"}

endpoint = "https://api.test.sabre.com/v1"

urlByService = "/auth/token?="
url = endpoint + urlByService
user = parameters["user"]
group = parameters["group"]
domain = parameters["domain"]
password = parameters["password"]
encodedUserInfo =  encodeBase64("V1:" + user + ":" + group + ":" + domain)
encodedPassword =  encodeBase64(password)
encodedSecurityInfo = encodeBase64(encodedUserInfo + ":" + encodedPassword)
data = {'grant_type':'client_credentials'}
headers = {'content-type': 'application/x-www-form-urlencoded ','Authorization': 'Basic ' + encodedSecurityInfo}

response = requests.post(url, headers=headers,data=data)

print "Post Request to: " + url
print response
print "Response Message: " + response.text

Regards,

Paulo Scardine

First get your credentials:

  1. Register in https://developer.sabre.com/member/register
  2. Sign in into https://developer.sabre.com
  3. 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'
    

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}

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'},
   ...
}

I'm pretty sure you're using this oauth library: https://pypi.python.org/pypi/oauth2 Despite being named "oauth2" it does not implement the OAuth 2 specification. Here is the best library I could find that provides OAuth 2 support and has documentation: http://requests-oauthlib.readthedocs.org/en/latest/oauth2_workflow.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!