NodeJS Facebook用户订阅源订阅:无效的OAuth访问令牌

时间:2015-02-25 13:12:24

标签: node.js facebook oauth

接近撕裂我的头发(作为一个充满激情的开发者,应该),所以希望我可以得到一些帮助。在阅读了大量论坛并筛选了facebook文档之后,这里有一个尝试订阅用户订阅源的日志文件。我真的只需要知道我运行的底层API是否应该在理论上起作用。这是我的日志:

*********************************************************
makeRequest - Host: graph.facebook.com - Path: /v2.2/{ app_id }/subscriptions?object=user
*********************************************************
Type: DELETE
*********************************************************
Wed Feb 25 2015 12:52:15 GMT+0000 (UTC) Server is listening on port 81
*********************************************************
makeRequest - Host: graph.facebook.com - Path: /oauth/access_token?client_id={ app_id }&client_secret={ app_secret }&grant_type=client_credentials
*********************************************************
Type: GET
*********************************************************
App Access Token: { app_token }
*********************************************************
makeRequest - Host: graph.facebook.com - Path: /v2.2/{ app_id }/subscriptions?&object=user&fields=feed&callback_url={ uri_encoded_ callback_url }&verify_token={ verify_token }&access_token={ app_token }
*********************************************************
Type: POST
*********************************************************
Success: {"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}

所以这里我首先尝试删除当前活动的所有订阅,然后使用API​​获取新的应用程序访问令牌,最后进行新的订阅。这是我初始化整个过程的功能:

function subscribe_facebook(http,https) {
    var oauth_obj   = getOauthObj('facebook');

    // First Delete
    var url         = oauth_obj.subscription_url;
    var host        = splitUrl(url,"host");
    var path        = splitUrl(url,"path");

    path += "?object=user";

    makeRequest(http,host,path,80,'DELETE',function(ret){
        // Get App Access Token
        url         = oauth_obj.access_token_url;
        host        = splitUrl(url,"host");
        path        = splitUrl(url,"path");

        path    +=  "?client_id="       +   oauth_obj.app_id;
        path    +=  "&client_secret="   +   oauth_obj.app_secret;
        path    +=  "&grant_type=client_credentials";

        makeRequest(https,host,path,443,'GET',function(ret){
            facebook_app_token = ret.toString().split('|');
            facebook_app_token = facebook_app_token[1];
            consoleLogger("App Access Token: " + facebook_app_token);

            subscribe(https,'facebook',443,'POST',facebook_app_token,myServer);
        });
    });
}

这是它输入的订阅功能:

function subscribe(http,type,port,method,access_token,myServer) {
    var oauth_obj = getOauthObj(type);
    var sub_post_data = "";
    switch (type) {
        case "facebook":
            sub_post_data = 
                "&object="          +   "user"+
                "&fields="          +   "feed"+
                "&callback_url="    +   encodeURIComponent(myServer + "/" + type) +
                "&verify_token="    +   "*****" +
                "&access_token="    +   access_token
            break;
    }

    var host = splitUrl(oauth_obj.subscription_url,"host");
    var path = splitUrl(oauth_obj.subscription_url,"path");
    path += "?" + sub_post_data;

    makeRequest(http,host,path,port,method,function(ret) {
        consoleLogger("Success: " + ret);
    });
}

我做了另外一个功能来维护社交媒体变量:

function getOauthObj(type) {
    var obj = {};
    var authorize_url = "";
    var access_token_url = "";
    var request_token_url = "";
    var subscription_url = "";

    var app_id = "";
    var app_secret = "";

    var main_domain = "";
    var scope = "";

    switch (type) {
        case "facebook":
            app_id      = '*****';
            app_secret  = '**********';

            main_domain         = 'https://graph.facebook.com/';
            authorize_url       = 'https://www.facebook.com/dialog/oauth';
            access_token_url    = main_domain + 'oauth/access_token';
            request_token_url   = main_domain + '2.2/me';
            subscription_url    = main_domain + "v2.2/" + app_id + "/subscriptions";
            scope               = 'read_stream';
            break;
    }
    obj.main_domain         = main_domain;

    obj.authorize_url       = authorize_url;
    obj.access_token_url    = access_token_url;
    obj.request_token_url   = request_token_url;
    obj.subscription_url    = subscription_url;

    obj.app_id              = app_id;
    obj.app_secret          = app_secret;

    return obj;
}

由于OAuth无效,我假设我没有将正确的参数传递给oauth API来执行此操作 - 有人可以引导我朝正确的方向发展吗?

1 个答案:

答案 0 :(得分:1)

好的,现在已经解决了。我在一些论坛上读到过关于不同Facebook API的详细信息,其中详细说明了使用应用程序ID,与管道连接,然后可以使用应用程序密钥代替访问令牌,但我不认为这可以在这里使用,但是事实证明它可以!

*********************************************************
makeRequest - Host: graph.facebook.com - Path: /v2.2/{ app_id }/subscriptions?&object=user&fields=feed&callback_url={ callback_url }&verify_token={ verify_token }&access_token={ app_id }|{ app_secret }

希望这有助于某人。

干杯。