简体   繁体   English

为什么我在 Microsoft Graph API 调用中收到“401 Unauthorized”?

[英]Why am I Getting "401 Unauthorized" for Microsoft Graph API Call?

I created the app in Tenant A and added it to Tenant B. I have granted the permissions in both tenants.我在租户 A 中创建了应用程序并将其添加到租户 B。我已授予两个租户的权限。 Why am I getting this response every time I make an API call to the app?为什么每次调用应用程序时都会收到此响应?

resulted in a `401 Unauthorized` response: {"error":{"code":"NoPermissionsInAccessToken","message":"The token contains no permissions, or permissions can not be un (truncated...)

Here is the PHP request that I'm making (I am using the client id and client secret from the app in Tenant A):这是我正在发出的 PHP 请求(我正在使用租户 A 中应用程序的客户端 ID 和客户端密码):

<?php

use League\OAuth2\Client\Provider\Exception\IdentityProviderException;

use Microsoft\Graph\Graph;

$guzzle = new \GuzzleHttp\Client();

$tenantId = 'common';
$clientId = 'ccc-ddd-fff';
$clientSecret = 'xxx-yyy-zzz';

$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';

try {
    $token = json_decode($guzzle->post($url, [
        'form_params' => [
            'client_id' => $clientId,
            'client_secret' => $clientSecret,
            'resource' => 'https://graph.microsoft.com/',
            'grant_type' => 'client_credentials',
        ],
    ])->getBody()->getContents());

    $accessToken = $token->access_token;

} catch (\Exception $e) {
    print $e->getMessage();
}

$graph = new Graph();
$graph->setAccessToken($accessToken);

try {
    print_r($graph->createRequest("GET", '/users/email@email.com/messages/xxxxxxxxxxxxx==')->execute());
} catch (\Exception $e) {
    print $e->getMessage();
}

Both tenants have these permissions granted: Permissions in Tenant A and B两个租户都授予了以下权限:租户 A 和 B 中的权限

Firstly, according to the api document , you'd better to add Mail.ReadBasic.All application permission, but I'm not sure if it influenced your calling here.首先,根据api 文档,你最好添加Mail.ReadBasic.All应用程序权限,但我不确定它是否影响你在这里调用。

Your issue mainly came from that you used $tenantId = 'common';您的问题主要来自您使用$tenantId = 'common'; , you should set it as the tenant name (such as xxx.onmicrosoft.com) you'd like to calling the api for. ,您应该将其设置为您要为其调用 api 的租户名称(例如 xxx.onmicrosoft.com)。 I mean that if you wanna call /users/xxx@tenantA.com/messages , you should put tenantA's tenant id/name to the $tenantId variable, but when you wanna call /users/yyy@tenantB.com/messages , you should set tenantB's.我的意思是,如果你想调用/users/xxx@tenantA.com/messages ,你应该把tenantA的租户id/name 放到$tenantId变量中,但是当你想调用/users/yyy@tenantB.com/messages时,你应该设置租户B。

Screenshot below showed the scenario when I used a token generated by common but not my tenant name.下面的屏幕截图显示了当我使用由common但不是我的租户名称生成的令牌时的场景。

在此处输入图像描述

401 Unauthorized error: Is your token valid? 401 未经授权的错误:您的令牌有效吗?

Make sure that your application is presenting a valid access token to Microsoft Graph as part of the request.确保您的应用程序在请求中向 Microsoft Graph 提供有效的访问令牌。 This error often means that the access token may be missing in the HTTP authenticate request header or that the token is invalid or has expired.此错误通常意味着 HTTP 身份验证请求 header 中可能缺少访问令牌,或者令牌无效或已过期。 We strongly recommend that you use the Microsoft Authentication Library (MSAL) for access token acquisition.我们强烈建议您使用 Microsoft 身份验证库 (MSAL) 获取访问令牌。 Additionally, this error may occur, if you try to use a delegated access token granted to a personal Microsoft account, to access an API that only supports work or school accounts (organizational accounts).此外,如果您尝试使用授予个人 Microsoft 帐户的委派访问令牌来访问仅支持工作或学校帐户(组织帐户)的 API,则可能会出现此错误。

You can always leverage jwt.ms to check claims on your token.您始终可以利用jwt.ms来检查您的令牌的声明。 Use this and check if you have the necessary permissions to call an API endpoint.使用它并检查您是否具有调用 API 端点的必要权限。

What I needed to do was prompt the Microsoft admin to grant permissions using a link similar to this one.我需要做的是提示 Microsoft 管理员使用与此类似的链接授予权限。 Merely adding the enterprise from the Microsoft仅从微软添加企业

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?scope=offline_access+openid+profile+User.Read+Mail.ReadWrite+Mail.Send
&response_type=code
&client_id=a62b0808-2b1f-4efc-a3d6-ad1223dc06a9
&redirect_uri=https://myurl/blah.html
&response_mode=query

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用php curl正确调用API? 我收到401的http状态码 - How do i call an API using php curl correctly? I am getting http status code of 401 我需要调用一个可以在 windows 中运行但在 linux 中不能运行的 API,我收到 401 错误 - I need to call an API which is working in windows but not in linux, I am getting 401 error 使用 Laravel Passport 设置 API - 为什么会出现未经授权的错误? - Setting up API with Laravel Passport - why am I getting an unauthorized error? 为什么我在尝试使用 Laravel 基本身份验证登录时不断收到 401 Unauthorized 错误? - Why I keep getting 401 Unauthorized error when trying to login with Laravel basic Auth? 401 未经 Adyen API 授权 - 401 Unauthorized on Adyen API 在进行Google + API调用时接收HTTP 401未经授权 - Receiving HTTP 401 Unauthorized when making a Google plus API call 访问 prestashop api 网络服务时出现“401 - 未经授权” - Getting "401 - unauthorized" when accessing the prestashop api webservice 为什么在尝试使用图形 API 发送电子邮件时出现以下不受支持的媒体错误? - Why I am getting the following unsupported media error while trying to send the email using graph api? PHP / FB API / GRAPH-为什么会收到未定义的偏移量错误? - PHP/FB API/GRAPH - Why am I getting an undefined offset error? 当我尝试使用PHP访问JIRA REST API时未经授权(401) - Unauthorized (401) when I try to access JIRA REST API with PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM