【问题标题】:Simple HTTPS GET from the AngelList API with NodeJS and Request使用 NodeJS 和 Request 从 AngelList API 进行简单的 HTTPS GET
【发布时间】:2016-06-15 07:56:12
【问题描述】:

我已设法授予用户访问权限,从而根据需要将access_token 获取到retrieve the data from the Angellist API

下一步是获取用户数据。正如我们可以从 Angellist API 中看到的那样,您可以通过经过身份验证的 HTTPS GET 请求来做到这一点:

使用令牌

必须进行任何需要身份验证的请求 使用 HTTPS,使用 HTTP 授权中提供的访问令牌 标头、请求正文或查询字符串。 AngelList 只支持 Bearer 令牌,定义在 https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-bearer-08。一个例子 使用查询字符串:

GET https://api.angel.co/1/me?
    access_token=...

为了在 NodeJS 中发出请求,我使用 npm 包 request 如下:

function testRequest(access_token) {
        
        console.log('test request');
        var urltest = "https://api.angel.co/1/me?access_token=" + access_token;
        
        request.get(urltest,
            {
              'auth': {
                'bearer': access_token
              }
            },
          function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    console.log('body', body)
                }
                console.log('body', body)
                console.log('error', error)
            }
        );
};

但是,我不断收到错误消息:

body {"success":false,"error":{"type":"unauthorized","message":"The authenticated user is not authorized for this request."}}

我做错了什么? access_token 是否需要转换或其他什么,即它不是不记名令牌?

【问题讨论】:

  • 你有没有尝试在邮递员或其他客户端中使用该api?
  • 当我通过 url 发送请求时返回相同的错误。不确定如何使用 Postman 添加不记名令牌?
  • 好的,我在 Postman 中添加了一个标头:Authorization with value "Bearer 12345token",它返回了同样的错误。

标签: node.js oauth https httprequest bearer-token


【解决方案1】:

我发现当尝试使用另一个 AngelList 帐户登录时,它可以工作。我之前尝试的帐户与我注册客户端应用程序的帐户相同。不知道为什么这会有所不同,但它确实有效。

【讨论】:

    【解决方案2】:

    您可以尝试如下发送请求吗:

            var optionsGetUserInfo = {
                method: 'GET',
                url: "https://api.angel.co/1/me?access_token=" + access_token",
                headers: {
                    'Authorization': 'Bearer ' + access_token
                    'Accept' : 'application/json',
                    'Content-Type' : 'application/json'
                }
            }
    
    request(optionsGetUserInfo, function (error, response, body) {
                        if (!error && response.statusCode == 200) {
                            etc...
    

    【讨论】:

    • 尝试从 url 中删除 access_token 。
    • 这行得通,但它返回了同样的错误:“经过身份验证的用户无权执行此请求。”
    • 我尝试了api.angel.co/1/me 'Authorization: Bearer someRandomStr' 并且我得到了“error: access denied”和“error_description: This access token is invalid or has been revokeed by the user”。所以,我假设它可以使用有效的访问令牌。
    • 访问令牌应该是正确的,因为我使用上述方法检索它。错误是:“...用户未授权”而不是“访问令牌无效”,这意味着我们可以假设 access_token 是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多