Search code examples
python-3.xbearer-token

Why I am getting invalid token error when I am giving the right token


I am trying to get data from Zendesk using Python. They have the following instruction in their API documentation. Though I am giving the right API token, I am getting an invalid authorization error.

Zendesk doc:

GET /v3/deals/custom_fields
Authorization: Bearer $TOKEN

My attempt:

import requests            
import json

domain = 'https://mydomain.zendesk.com/v3/deals/custom_fields.json'
api_token= 'apikey'

payload = ""
headers = {
    'Accept': "application/json",
    'Content-Type': "application/json",
    'Authorization': "Bearer "+api_token
    }
response1=requests.request("GET", domain, data=payload, headers=headers)
response1.json()

Error:

{'error': 'invalid_token', 'error_description': 'The access token provided is expired, revoked, malformed or invalid for other reasons.'}


Solution

  • This is how I connect with a token to Zendesk API :

    Authorization: Basic {email_address}/token:{api_token}
    

    Don't use Bearer but Basic instead.

    We need to base64 encode {email_address}/token:{api_token}

    Example :

    If you have :

    [email protected]/token:6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv
    

    Then :

    Authorization: Basic amRvZUBleGFtcGxlLmNvbS90b2tlbjo2d2lJQldiR2tCTW8xbVJETXVWd2t3MUVQc05rZVVqOTVQSXoyYWt2
    

    Basic authentication is used for API tokens. As described in Basic authentication above, the credentials must be sent with the request in an Authorization header.

    source : https://developer.zendesk.com/api-reference/introduction/security-and-auth/#api-token