OneDrive 代码流 公共客户端无法发送客户端密码 - Node.js



嘿伙计们,我想创建对我的onedrive帐户的访问权限,以通过node.js从我的家用PC窗口上传文件。

我在 https://apps.dev.microsoft.com
上创建了一个应用程序 我还在那里创建了一个客户端密码,并添加了一个 Web 平台,并将重定向 url 从 localhost 更改为 https://login.live.com/oauth20_desktop.srf

然后我在浏览器中使用了这个链接 https://login.live.com/oauth20_authorize.srf?client_id=ab82982b-4dxxxxxxxxxxxxxxxxx&scope=files.readwrite.all&response_type=code

我的浏览器中的 URL 更改为 https://login.live.com/oauth20_desktop.srf?code=M494a5b9fxxxxxxxxxxxxxxxxxxxxxxx&lc=1031

然后我发出一个 POST 请求,就像他们在 https://dev.onedrive.com/auth/graph_oauth.htm 上所说的那样

request({
uri: "https://login.microsoftonline.com/common/oauth2/v2.0/token?"
+ "&client_id=ab82982b-4dbe-4c6b-a1fe-2d60d01709fd&"
+ "client_secret=TkYZhYyuEiSoqhCxbh4Dqh3"
+ "&code=M494a5b9f-5577-3454-a78c-cef649a512c0"
+ "&grant_type=authorization_code",
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function(error, response, body) {
console.log('body: ', body);
});

但输出是

body:  {"error":"invalid_request","error_description":"AADSTS90014: The 
request body must contain the following parameter: 'grant_type'.rnTrace 
ID:
de2c2dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrnCorrelation ID: 
de2f8b83xxxxxxxxxxxxxxxxxxxxxxxxxrnTimestamp: 2017-07-31 13:40:52Z","error_codes":[90014]
,"timestamp":"2017-07-31 13:40:52Z","trace_id":"de2c2da2xxxxxxxxxxxxxxxxxxx","correlation_id":"de2f8b8xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

请帮助我在这个 API 令牌的东西上挣扎..

从下面的评论中编辑我也改变了

request.post({url:'https://login.microsoftonline.com/common/oauth2/v2.0/token', form: {
redirect_uri: 'https://login.live.com/oauth20_desktop.srf',
client_id: 'abf3247c-d56a-xxxxxxxxxxxxxxxxxxxxx',
client_secret: '3o6xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
code: 'M8aad1bcf-xxxxxxxxxxxxxxxxxxxxxxxxxx',
grant_type: 'authorization_code'
}
}, function(err,httpResponse,body){ /* ... */ 
console.log('err: ' + err)
console.log('body: ' + body)
})

但是现在我得到"错误":"invalid_request","error_description":"AADSTS90023:公共客户端无法发送客户端密码。

我用谷歌搜索并读到我无法使用桌面应用程序发出客户端秘密请求。但我在 https://apps.dev.microsoft.com 创建了一个 Web 应用程序

此外,我从请求中删除了客户端密钥,我收到重定向URL错误的错误。请给我发送工作代码示例,我现在为此苦苦挣扎了几天..

这太:D难了啊请帮忙

你的这个问题被打开了吗?您似乎想要检索访问令牌并刷新令牌。如果我误解了你的问题,我很抱歉。

我认为您修改的用于检索访问令牌的脚本没有错。请再次确认授权流程。

  1. https://apps.dev.microsoft.com/添加应用程序
  2. 输入应用程序名称。在这种情况下,请勿使用引导式设置
  3. 创建应用程序机密。
  4. 平台是网络。在这种情况下,重定向 URLhttp://localhost
  5. https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=### Application ID ###&scope=offline_access%20files.readwrite.all&response_type=code&redirect_uri=http://localhost中检索代码
    • 请将上述 URL 移植到您的浏览器,并从重定向的 URL 中检索代码。
    • 在这里,为了上传文件,它在范围内包括files.readwrite.all
    • 可以通过将offline_access包含在范围中来检索刷新令牌。
  6. 运行以下脚本以检索访问令牌和刷新令牌。

脚本:

request.post({
url:'https://login.microsoftonline.com/common/oauth2/v2.0/token',
form: {
redirect_uri: 'http://localhost',
client_id: 'abf3247c-d56a-xxxxxxxxxxxxxxxxxxxxx',
client_secret: '3o6xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
code: 'M8aad1bcf-xxxxxxxxxxxxxxxxxxxxxxxxxx',
grant_type: 'authorization_code'
}
}, function(err,httpResponse,body){
console.log('body: ' + body)
});

响应:

您可以检索以下响应。

{
"token_type": "Bearer",
"scope": "Files.ReadWrite.All",
"expires_in": 3600,
"ext_expi
res_in": 0,
"access_token": "#####",
"refresh_token": "#####"
}

如果这不是您的解决方案,我很抱歉。

用于从刷新令牌检索访问令牌的脚本:

request.post({
url:'https://login.microsoftonline.com/common/oauth2/v2.0/token',
form: {
redirect_uri: 'http://localhost',
client_id: 'abf3247c-d56a-xxxxxxxxxxxxxxxxxxxxx',
client_secret: '3o6xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
refresh_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
grant_type: 'refresh_token'
}
}, function(err,httpResponse,body){
console.log('body: ' + body)
});

最新更新