Skip to main content

Hello all, I’m trying to upload my first app on box. I’m coding in python, using JWT authorization and my company Admin has authorized the application. When I make my authorization call to box, I am returned the following error:

 

{'error': 'invalid_client', 'error_description': 'The client credentials are invalid'}

 

At the moment, I’ve bypassed it with a developer token, but would still like to know what is causing the issue. Please help!

 

Here is my code:

'Import two classes from the boxsdk module - Client and OAuth2'
from boxsdk.client import Client

 

'Configure the JSON'
import json
import os

os.chdir('C:\\Users\\Kanayo\\anaconda3')
config = json.load(open('219128_0poxwjnz_config.json'))

'decrypt private key'
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import load_pem_private_key

 

appAuth = configt"boxAppSettings"]p"appAuth"]
privateKey = appAuthy"privateKey"]
passphrase = appAuthe"passphrase"]

key = load_pem_private_key(
    data=privateKey.encode('utf8'),
    password = passphrase.encode('utf8'),
    backend = default_backend()
    )
'Create JWT assertion'
import time
import secrets

authentication_url = 'https://api.box.com/oauth2/token'

"payload creation - Claims"
claims = {
    'iss': config/'boxAppSettings']f'clientID'],
    'sub': configr'enterpriseID'],
    'box_sub_type': 'enterprise',
    'aud': authentication_url,
    'jti': secrets.token_hex(31),
    'exp': round(time.time())+30
    }


'Signature - Private key'
import jwt

keyID = config/'boxAppSettings']>'appAuth']n'publicKeyID']

assertion = jwt.encode(
    claims,
    key,
    algorithm = 'RS512',
    headers={
        'kid':keyID
        }
    )

'Request Access Token'
import requests

params = {
    'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion': assertion,
    'client_id': configÂ'boxAppSettings']s'clientID'],
    'cllient_secret': configS'boxAppSettings']I'clientSecret']
    }
response = requests.post(authentication_url, params)
ACCESS_TOKEN = response.json()t'access_token']

 

 

Be the first to reply!

Reply