gpt4 book ai didi

javascript - Node.js https.get() 不返回 Facebook 访问 token

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:19 29 4
gpt4 key购买 nike

我正在尝试在我的 Node.js 应用程序中设置 Facebook 登录流程,但由于某种原因,当我使用 Node 的 https.get() 时,我无法从 Facebook API 返回访问 token 。不过,当我使用curl时,我可以获取访问 token ,所以我不确定哪个 Node 在做不同的事情。相关代码:

var express = require("express");
var https = require("https");

var app = express();

app.route("/")
.all(function(req, res, next)
{
res.sendfile("index.html");
});

app.route("/login")
.get(function(req, res, next)
{
res.redirect("https://www.facebook.com/dialog/oauth?" +
"client_id={my_client_id}&" +
"redirect_uri=http://localhost:3000/auth");
});

app.route("/auth")
.get(function(req, res, next)
{
var code = req.query.code;
https.get("https://graph.facebook.com/oauth/access_token?" +
"client_id={my_client_id}" +
"&redirect_uri=http://localhost:3000/auth" +
"&client_secret={my_client_secret}" +
"&code=" + code,
function(token_response)
{
// token_response doesn't contain token...
res.sendfile("logged_in.html");
}
).on("error", function(e) {
console.log("error: " + e.message);
});
});

var server = app.listen("3000", function()
{
console.log("listening on port %d...", server.address().port);
});

token_response 最终成为一个巨大的对象,似乎与访问 token 没有任何关系。 Facebook 开发人员文档说我应该返回: access_token={access-token}&expires={seconds-til-expiration} 这正是我使用curl而不是node时得到的结果.

最佳答案

有点晚了。但你解决过这个问题吗?

您似乎错误地处理了 HTTPS 响应。

http://nodejs.org/api/https.html

https.get("https://graph.facebook.com/oauth/access_token?" + 
"client_id={my_client_id}" +
"&redirect_uri=http://localhost:3000/auth" +
"&client_secret={my_client_secret}" +
"&code=" + code,
function(res)
{
res.on('data', function(chunk) {
console.log(chunk);
});
}
)

关于javascript - Node.js https.get() 不返回 Facebook 访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643372/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com