Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Invalid JSON. A token was not recognized in the JSON content.

I am building a python script for automating add user to datasource, as I debug the script, I am getting this error.


full error:
{'error': {'code': 'BadRequest', 'message': 'Bad Request', 'details': [{'message': 'Invalid JSON. A token was not recognized in the JSON content.', 'target': 'userAccessRightEntry'}]}}

 

===

 

    context = adal.AuthenticationContext(authority, validate_authority=True)

    token = context.acquire_token_with_username_password(resource, username , password, clientid)

    accessToken = token['accessToken']

    #print('this is the token:', accessToken)   #after this line the code is throwing me the error above.

    headers = {'Authorization': 'Bearer {}'.format(accessToken), 'Content-Type': 'application/json'}

    body = { 'emailAddress' : '_@email.com', 'AccessRight' : 'Read'}

 

    try:

        response = requests.post(_uri, headers=headers, data=body)

        results = response.json()

        print ("response:", results)

 

    except Exception as e: 

        print (str(e))

3 REPLIES 3
Anonymous
Not applicable

@Anonymous 

 

This doesnt resolve the issue.

I tried changing the header before posted this issue with the header format you provided:

headers = {
"Authorization": "Bearer " & str(accessToken),
"Content-Type": "application/json",
)

 

 

 

Anonymous
Not applicable

HI @Anonymous,

How about directly using fixed tokens string in your post step? 
BTW, you can also try to change the variable name if this issue is related to the conflict of variable and function parameter names.

 

context = adal.AuthenticationContext(authority, validate_authority=True)
token = context.acquire_token_with_username_password(
    resource, username, password, clientid
)
accessToken = token["accessToken"]
# print('this is the token:', accessToken)   #after this line the code is throwing me the error above.
_headers = {
    "Authorization": "Bearer xxxxxxxxxxxxxx",
    "Content-Type": "application/json",
}
body = {"emailAddress": "_@email.com", "AccessRight": "Read"}

try:
    response = requests.post(_uri, headers=_headers, data=body)
    results = response.json()
    print("response:", results)
except Exception as e:
    print(str(e))

 

Regards,

Xiaoxin Sheng

Anonymous
Not applicable

HI @Anonymous,

I think this should more relate to the 'headers' structure, you can try to use the following codes if it works on your side.

context = adal.AuthenticationContext(authority, validate_authority=True)
token = context.acquire_token_with_username_password(
    resource, username, password, clientid
)
accessToken = token["accessToken"]
# print('this is the token:', accessToken)   #after this line the code is throwing me the error above.
headers = {
    "Authorization": "Bearer " & str(accessToken),
    "Content-Type": "application/json",
}
body = {"emailAddress": "_@email.com", "AccessRight": "Read"}

try:
    response = requests.post(_uri, headers=headers, data=body)
    results = response.json()
    print("response:", results)
except Exception as e:
    print(str(e))

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.