Microsoft Graph REST API 无效的客户端密码

Microsoft Graph REST API invalid client secret

我需要拨打以下 POST 电话。但是,即使我提供了正确的客户端 ID 和机密 ID,我的呼叫也会被拒绝。

    curl POST https://login.microsoftonline.com/f02....e3/oauth2/token
 -H 'Content-Type: application/x-www-form-urlencoded'  --data 'grant_type=authorization_code&redirect_uri=https://requestb.in/ac&
source=https://graph.microsoft.com&client_id=1e1....-913d9
&client_secret=YmbSFYz.....4Uk=&scope=mail.read&code=AaAAA........on0a569'

这是我收到的错误:

    curl: (6) Could not resolve host: POST
    {"error":"invalid_client","error_description":"AADSTS70002: 
Error validating credentials. AADSTS50012: Invalid client secret is
 provided.\r\nTrace ID: 78d...a2b\r\nCorrelation ID: 
01....ab2\r\nTimestamp: 2016-12-14 01:46:47Z","error_codes":[70002,50012],"timestamp":"2016-12-14 01:46:47Z","trace_id":"78d....a2b","correlation_id":"018.....ab2"}

我该如何解决这个问题?

编辑: 我正在尝试在 this documentation

中实现第二部分(即获取令牌)

您提供的 post 正在利用 AAD V2 端点。但是根据您的代码片段,您使用的是 V1 端点 https://login.microsoftonline.com/f02....e3/oauth2/token。关于通过 V1 端点获取访问令牌,您可以参考 https://graph.microsoft.io/en-us/docs/authorization/app_authorization 了解更多详情。

对于 V2 授权端点,您可以查看您正在使用的端点:

GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?...

POST https://login.microsoftonline.com/common/oauth2/v2.0/token

并且还需要v2.0广告应用程序:

This article assumes a v2.0 registration, so you'll register your app on the Application Registration Portal.

我正在使用 npm 包:

Microsoft Azure Active Directory Passport.js Plug-In

使用 v1 tenant-specific 端点。

我最初在成功登录 Microsoft 后收到相同的错误消息:"Invalid Client Secret"。

我使用 "Application ID" 作为客户端密码。然后我发现您需要转到 Microsoft Azure 门户中的 "Settings" 选项卡

Microsoft Azure

并创建一个新的 "Key"。你给钥匙起什么名字并不重要。当您点击 "Save" 按钮时,该键的值将被填写到网络表单中。立即复制此内容,因为如果刷新网页,它将不会再次显示。

这是您需要添加到配置中的 "Client Secret"。

这是由于 client_secret。它可能包含特殊字符。

encodeURIComponent() 函数对 URI 组件进行编码。 此函数对特殊字符进行编码。此外,它还对以下字符进行编码:, / ? : @ & = + $ #

使用下面的:

encodeURIComponent(client_secret);

我今天遇到了同样的问题,在@muthu 的帮助下我解决了它。 当在 Azure 中通过应用程序注册生成客户端密码时。秘诀很简单。但是什么时候在 rest 调用中使用它,你必须对它进行 urlencode。 使用像视觉代码或其他编码器。但必须对其进行编码,否则会出现错误

AADSTS7000215:提供的客户端密码无效。

您需要在 POST 正文中对 client_secret 进行 base 64 编码。

例如(使用 curl):

# Authn details
LoginURL='https://login.microsoftonline.com'
TenantDomain='********.onmicrosoft.com'
ClientID='********'
ClientSecret='********'

# Endpoint details
Resource='https://graph.microsoft.com'
TenantGUID="********"

# Authenticate with OAuth v1
URL="$LoginURL/$TenantDomain/oauth2/token?api-version=1.0"
json=`
curl \
        -s \
        -k \
        -X POST \
        -H "Content-Type: application/x-www-form-urlencoded" \
        -u "$ClientID:$ClientSecret" \
        -d "grant_type=client_credentials" \
        -d "resource=$Resource" \
        -d "client_id=$ClientID" \
        --data-urlencode "client_secret=$ClientSecret" \
        $URL \
| python -m json.tool
`

access_token=`echo $json | python -c 'import sys, json; print json.load(sys.stdin)["access_token"]'`
token_type=`echo $json | python -c 'import sys, json; print json.load(sys.stdin)["token_type"]'`
#echo "access_token:$access_token"
#echo "token_type:$token_type:"

# Access resource
URL="$Resource/v1.0/directoryRoles/$TenantGUID/members"
curl \
        -s \
        -k \
        -X GET \
        -H "Content-Type: application/x-www-form-urlencoded" \
        -H "Authorization: $token_type $access_token" \
        $URL \
| python -m json.tool

以我为例: 在配置中,您必须使用客户端密码“Value”,而不是 ID。 此值仅在生成时可见。所以你可以 copy/paste 它,就在这一刻。

参见:https://docs.microsoft.com/en-us/answers/questions/370508/getting-34invalid-client-secret-is-provided34-erro.html