[Python] How to get access Token from Amadesu API on Python

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I can't get access token from Amadeus Flight API authentication process using requests and oauth_lib. https://developers.amadeus.com/self...eys/authorization/#requesting-an-access-token

    Using Requests

    import requests

    API_KEY = "twAtndd5V..."
    API_SECRET = "xtj..."
    AUTH_ENDPOINT = "https://test.api.amadeus.com/v1/security/oauth2/token/"
    head = {"Content-Type" : "application/x-www-form-urlencoded"}
    response = requests.post(
    url=AUTH_ENDPOINT,
    json={"grant_type" : "client_credentials",
    "client id" : API_KEY,
    "client_secret" : API_SECRET},
    headers=head
    )
    print(response.text)


    {
    "error":"invalid_request",
    "error_description": "No access_token parameter needed for this method",
    "code": 38187,
    "title": "Invalid parameters"
    }


    Try with oauth2 https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow

    from requests_oauthlib import OAuth2Session
    from oauthlib.oauth2 import BackendApplicationClient

    client = BackendApplicationClient(client_id=API_KEY)
    oauth = OAuth2Session(client=client)
    token = oauth.fetch_token(token_url=AUTH_ENDPOINT, client_id=API_KEY, client_secret=API_SECRET)


    token = oauth.fetch_token(token_url=AUTH_ENDPOINT, client_id=API_KEY, client_secret=API_SECRET)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\asdf\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests_oauthlib\oauth2_session.py", line 406, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
    File "C:\Users\asdf\AppData\Local\Programs\Python\Python312\Lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 427,
    in parse_request_body_response
    self.token = parse_token_response(body,
    scope=scope)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\asdf\AppData\Local\Programs\Python\Python312\Lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 441, in parse_token_response
    validate_token_parameters(params)
    File "C:\Users\asdf\AppData\Local\Programs\Python\Python312\Lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 448, in validate_token_parameters
    raise_from_error(params.get('error'), params)
    File "C:\Users\asdf\AppData\Local\Programs\Python\Python312\Lib\site-packages\oauthlib\oauth2\rfc6749\errors.py", line 399, in raise_from_error
    raise cls(**kwargs)
    oauthlib.oauth2.rfc6749.errors.InvalidClientIdError: (invalid_request) No access_token parameter needed for this method

    Continue reading...

Compartilhe esta Página