【问题标题】:Can not get Instagram Access Token using request module in node无法使用节点中的请求模块获取 Instagram 访问令牌
【发布时间】:2016-04-27 06:00:14
【问题描述】:

我正在使用请求和 Instagram api 构建一个简单的节点应用程序,并尝试在 oauth 过程的最后一步调用 instagram 访问令牌。出于某种原因,Instagam 的 api 以 400 响应,说我的 client_id 在我的请求正文中明确指定时没有指定。这是instagram oauth 流程的链接:https://www.instagram.com/developer/authentication/

这是我的请求代码:

var options = {
  url: "https://api.instagram.com/oauth/access_token",
  method: 'POST',
  oauth: {
    "client_id": process.env.CLIENT_ID,
    "client_secret": process.env.CLIENT_SECRET
  },
  json: {
    "client_id": process.env.CLIENT_ID,
    "client_secret": process.env.CLIENT_SECRET,
    "grant_type" : "authorization_code",
    "code" : response.req.query.code,
    "redirect_uri" : 'http://127.0.0.1:5000/redirect'
  }
};

request(options, function ( error, response ) {
  if(error) { console.log(error); }
  else { console.log(JSON.stringify(response)); }
});

回复如下:

{"statusCode":400,"body":{"code":400,"error_type":"OAuthException","error_messag
e":"You must provide a client_id"},"headers":{"content-language":"en","expires":
"Sat, 01 Jan 2000 00:00:00 GMT","vary":"Cookie, Accept-Language","pragma":"no-ca
che","cache-control":"private, no-cache, no-store, must-revalidate","date":"Wed,
 27 Apr 2016 05:37:03 GMT","content-type":"application/json","set-cookie":["csrf
 token=456dc5df05f5b0aad286723004e395fe; expires=Wed, 26-Apr-2017 05:37:03 GMT; M
ax-Age=31449600; Path=/","mid=VyBP_wAEAAGhT9YCRDGVBM6tksC7; expires=Tue, 22-Apr-
2036 05:37:03 GMT; Max-Age=630720000; Path=/"],"connection":"close","content-len
gth":"94"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"hos
t":"api.instagram.com","port":443,"hostname":"api.instagram.com","hash":null,"se
arch":null,"query":null,"pathname":"/oauth/access_token","path":"/oauth/access_t
oken","href":"https://api.instagram.com/oauth/access_token"},"method":"POST","he
aders":{"accept":"application/json","content-   type":"application/json","content-l
ength":223,"Authorization":"OAuth oauth_client_id=\"[THE ACUAL RESPONSE       DISPLAYS ID HERE]\",oauth_client_secret=\"[THE ACTUAL RESPONSE DISPLYS SECRET     HERE]\",oauth_nonce=\"cc
173578667148d1bdf341926b9e2cd0\",oauth_signature_method=\"HMAC-SHA1\",oauth_time
stamp=\"1461735413\",oauth_version=\"1.0\",oauth_signature=\"zeQxrYVNSsveztczszW
FiY3jGz8%3D\""}}}

【问题讨论】:

    标签: node.js oauth request instagram instagram-api


    【解决方案1】:

    我认为环境变量 process.env.CLIENT_ID 没有价值。 尝试使用硬编码字符串而不是 process.env.CLIENT_ID 和 process.env.CLIENT_SECRET

    【讨论】:

    • 没有环境变量记录为我的 id 和 secret 的字符串,因此它们绝对不是未定义的。这可能是我在请求正文中指定它们的方式吗?
    【解决方案2】:

    我在使用其他 API 时也为此苦苦挣扎了好几天。但这适用于所有 api oAuth 2 请求。

    request.post({url:'https://api.instagram.com/oauth/access_token', form: {
        redirect_uri: 'http://localhost/dashboard',
        client_id: 'xxxxxxxxxxx',
        client_secret: 'xxxxxxxxxxxxx',
        code: 'xxxxxxxxxxxxxxx',
        grant_type: 'authorization_code'
    }
    }, function(err,httpResponse,body){ /* ... */ 
    console.log('err: ' + err)
    console.log('body: ' + body)
    })
    

    我在注册应用程序http://localhost/dashboard时也将其用作重定向网址
    不要只使用 http://localhost,因为如果您使用 XAMPP,那么您将始终被重定向到 http://localhost/dashboard,这意味着您无法从
    https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

    获取代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多