cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
codemici
New Member

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
codemici
New Member

@v-shex-msft 

 

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",
)

 

 

 

HI @codemici,

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
v-shex-msft
Community Support
Community Support

HI @codemici,

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are You Ready?

Test your skills now with the Cloud Skills Challenge.

Top Solution Authors