2016-03-02 94 views
0

我已经设法授予用户的访问权,从而根据需要获得access_tokenretrieve the data from the Angellist API使用NodeJS和请求从AngelList API进行简单的HTTPS GET

下一步是获取用户数据。我们可以从Angellist API阅读,您通过身份验证的HTTPS GET请求做到这一点:

Use the Token

Any requests that require authentication must be made using HTTPS, with the access token provided in the HTTP Authorization header, request body or query string. AngelList only supports Bearer tokens, as defined in http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-08 . An example using the query string:

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

要在的NodeJS的要求,我用的是故宫包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是否需要转换或者什么,即它不是一个不记名令牌?

+0

您是否尝试过在邮递员或其他客户端使用api? –

+0

当我通过一个url发送请求时,返回相同的错误。不知道如何用邮差添加持票人代币? – JohnAndrews

+0

好吧,我在Postman中添加了一个标头:授权值“Bearer 12345token”,并返回相同的错误。 – JohnAndrews

回答

1

你可以尝试如下发出请求:

 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... 
+0

谢谢。给出502错误:s – JohnAndrews

+0

尝试从url中移除access_token。 –

+0

工作正常,但它返回了同样的错误:“经过身份验证的用户未获得此请求的授权。” – JohnAndrews

0

我已经发现,尝试与另一AngelList帐户登录时,那么它的工作原理。我之前尝试过的帐户与我注册客户端应用程序时的帐户相同。不知道为什么这有所作为,但它的工作原理。