簡體   English   中英

Node.js https.get()未返回Facebook訪問令牌

[英]Node.js https.get() not returning Facebook access token

我正在嘗試在node.js應用程序中設置Facebook登錄流程,但是由於某些原因,當我使用節點的https.get()時,無法從Facebook API返回訪問令牌。 但是,當我使用curl時,我可以獲得訪問令牌,所以我不確定哪個節點在做不同的事情。 相關代碼:

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最終是一個似乎與訪問令牌無關的巨大對象。 Facebook開發人員文檔說我應該回來: access_token={access-token}&expires={seconds-til-expiration} ,這正是我使用curl而不是節點時得到的。

有點晚了。 但是你有沒有解決這個問題?

似乎您在錯誤地處理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);
            });
        }
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM