0

I am trying to connect to the Bynder API using Python and the documentation https://bynder.docs.apiary.io/ but I can make it to connect to the Analytics endpoint https://bynder.docs.apiary.io/#reference/analytics because I received a 401 response

I have the following code :

from bynder_sdk import BynderClient
    
headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Origin': 'Allowed domain for cross-domain requests. Only required if any domains were specified in \'Set HTTP access control (CORS)\' for the OAuth application.',
      'Authorization': 'Basic b64("abcd":"abcd")'
    }
    
    bynder_client = BynderClient(
        domain='abcd.com',
        redirect_uri='https://abcd.com/v7/analytics/api/v1/asset/view',
        client_id='abcd',
        client_secret='abcd',
        scopes ="offline analytics.api:read",
        grant_type="client_credentials"
    )
    
    print(bynder_client.get_authorization_url())
    print(bynder_client.get_authorization_url()[1])

params = {"limit":"100", "fromDateTime":"2022-01-01T01:00","toDateTime":"2022-06-01T01:00" }

api_call_headers = {'Authorization': 'Token ' + bynder_client.get_authorization_url()[1]}
api_call_response = requests.get("https://abcd.abcd.com/v7/analytics/api/v1/asset/view", headers=api_call_headers, params=params, verify=False)

can someone help me to understand how to Autorise using OAuth 2.0 the Client ID and Client Secret and use the Analytics endpoint? I have all the details in the bynder_client = BynderClient()

Thanks

1 Answers1

0

For anyone reference this his how I ended up making my code work:

endpoint_api_url="https://abcd.abcd.com/v7/analytics/api/v1/asset/download"
auth_server_url = "https://abcd.abcd.com/v6/authentication/oauth2/token"
client_id = 'abcd'
client_secret='abcd'
token_req_payload = {'grant_type': 'client_credentials'}

token_response = requests.post(auth_server_url,
data=token_req_payload, verify=False, allow_redirects=False,
auth=(client_id, client_secret))

print(token_response.status_code)

if token_response.status_code ==200:
    print("Successfuly obtained a new token")
    print(token_response.text)
    tokens = json.loads(token_response.text)
    token = tokens['access_token']
    print(token)

else:
    print("Failed to obtain token from the OAuth 2.0 server", file=sys.stderr)
    sys.exit(1)
    
params = {"limit":"100", "fromDateTime":"2022-01-01T01:00","toDateTime":"2022-06-01T01:00" }

api_call_headers = {'Authorization': 'Bearer ' + token}
api_call_response = requests.get(endpoint_api_url, headers=api_call_headers, params=params,  verify=False)
print(api_call_response.text)