代码之家  ›  专栏  ›  技术社区  ›  t33n

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

  •  1
  • t33n  · 技术社区  · 7 年前

    嘿,伙计们,我想创建对我的onedrive帐户的访问权限,通过节点上传文件。我的家庭电脑窗口的js。

    我在上创建了一个应用程序 https://apps.dev.microsoft.com
    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

    然后我像他们说的那样发了一个帖子 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'.\r\nTrace 
    ID:
    de2c2dxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\nCorrelation ID: 
    de2f8b83xxxxxxxxxxxxxxxxxxxxxxxxx\r\nTimestamp: 2017-07-31 13:40:52Z","error_codes":[90014]
    ,"timestamp":"2017-07-31 13:40:52Z","trace_id":"de2c2da2xxxxxxxxxxxxxxxxxxx","correlation_id":"de2f8b8xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
    

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

    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)
    })
    

    我在谷歌上搜索了一下,发现我不能用桌面应用程序向客户提出保密请求。但我在

    此外,我还从请求中删除了客户机机密,因为重定向url错误。请寄给我工作代码的例子,我现在挣扎了几天。。

    这太难了aaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhh:D请帮忙

    1 回复  |  直到 7 年前
        1
  •  1
  •   Tanaike    7 年前

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

    1. 在添加应用程序 https://apps.dev.microsoft.com/
    2. 创建应用程序机密。
    3. 平台是web。在这种情况下,重定向URL为 http://localhost
    4. 从检索代码 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
    5. 运行以下脚本以检索访问令牌并刷新令牌。

    脚本:

    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)
    });