首页 > 解决方案 > 当我尝试获取访问令牌时出现“为 'redirect_uri' 提供的值无效”错误

问题描述

当我尝试获取令牌时,出现错误:

The provided value for the 'redirect_uri' is not valid. The value must exactly match the redirect URI used to obtain the authorization code.

我的重定向 uri 完全匹配,所以我不明白为什么会这样。

$TOKEN_ENDPOINT         = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
$params = array(
    'grant_type'              => 'authorization_code',
    'code'                    => $azureCode,
    'clientId'                => '7c09ab71-***-****-****-53d7c4438112',
    'clientSecret'            => 'bnot*******20*[',
    'redirect_uri'            => 'https://testing.****.com/outlookOauthCallback.php',
    'urlAuthorize'            => $AUTHORIZATION_ENDPOINT,
    'urlAccessToken'          => $TOKEN_ENDPOINT,
    'urlResourceOwnerDetails' => '',
    'scope'                   => 'Calendars.ReadWrite User.Read'
);
$response = $client->getAccessToken($TOKEN_ENDPOINT, 'authorization_code', $params);

在此处输入图像描述

(相信我,我变成星星的部分完全一样,因为我是复制粘贴的)

即使在我返回参数的响应中,它也是完全相同的:

Array
(
    [grant_type] => authorization_code
    [code] => M51b1b*****-daeec54627b2
    [clientId] => 7c09ab71-a*****d7c4438112
    [clientSecret] => bnotxds&*&QB***cVLF20*[
    [redirect_uri] => https://testing.****.com/outlookOauthCallback.php
    [urlAuthorize] => https://login.microsoftonline.com/common/oauth2/v2.0/authorize
    [urlAccessToken] => https://login.microsoftonline.com/common/oauth2/v2.0/token
    [urlResourceOwnerDetails] => 
    [scope] => Calendars.ReadWrite User.Read
)

那么它怎么还会给我这个错误呢?我在这里想念什么?

标签: authenticationoauth-2.0azure-active-directoryoutlook-restapi

解决方案


您需要在请求 url 中指定 redirect_uri。就像是

var href = 'login.microsoftonline.com/common/oauth2/…'; href += client_id + '&resource=webdir.online.lync.com&redirect_uri=' + window.location.href; 

推荐阅读