简体   繁体   中英

Microsoft Graph API Authentication - Access token is empty

I'm trying to get a bearer token to upload a file to Microsoft Teams. When doing a post request to

https://graph.microsoft.com/{tenantId)/oauth2/v2.0/token
body: 
    client_id, 
    scope= https://graph.microsoft.com/.default, 
    grant_type= authorization_code, 
    client_secret & redirect uri (https://myenvironment.com/callback)

code : I got this by doing a request to

https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/authorize?client_id={client_id}&response_type=code&redirect_uri={redirect_uri}&response_mode=query&scope=https://graph.microsoft.com/.default

I get a 401 Unauthorized with error code InvalidAuthenticationToken and error message Access token is empty .

I'm not sure which api you wanna call here, but I can show you an example. For instance, I want to call this api to upload a file on behalf of me.

Then I need to give api permission for calling this api, you can see api permission here.

在此处输入图像描述

Then open the browser to send a request like this

https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize?
client_id=client_id
&response_type=code
&redirect_uri=http%3A%2F%2Flocalhost:8080%2F
&response_mode=query
&scope=Files.ReadWrite Files.ReadWrite.All Sites.ReadWrite.All
&state=12345

Then you will get a code in the url, then cope the code.

Post: https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token

request header
Content-Type:application/x-www-form-urlencoded

request body
client_id:
client_secret:
code: copy_from_browser_url
redirect_uri:http://localhost:8080/
grant_type:authorization_code
scope:Files.ReadWrite Files.ReadWrite.All Sites.ReadWrite.All

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM