另一个应用程序安装

时间:2012-02-08 16:47:20

标签: facebook-graph-api

我正在尝试检查我的应用程序是否在特定的粉丝页面上安装了一个应用程序,如果没有安装,我想安装它.. 由于某种原因,我无法获取页面访问令牌。

我能够获取用户访问令牌和用户ID,但页面访问令牌返回“Exception”“Unknown fields:access_token”。

这里有什么问题? 我正在使用这些资源: http://developers.facebook.com/docs/reference/api/page/#tabs https://developers.facebook.com/docs/authentication/

 <html>
    <head>
    </head>
    <body>
      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : '188474844587139',
            status     : true, 
            cookie     : true,
            xfbml      : true,
            oauth      : true,
          });

          FB.getLoginStatus(function(response) {
              if (response.status === 'connected') {
                var uid = response.authResponse.userID;
                var userAccessToken = response.authResponse.accessToken;
                console.log("User Access Token: "+userAccessToken);
                console.log("User ID: "+uid);

                // get page access token
                FB.api('https://graph.facebook.com/'+uid+'/accounts?access_token='+userAccessToken, function(response) {
                  console.log(response);

                    var pageAccessToken = 'Need to get from the response...';

                    // get information if user got this app
                    FB.api("https://graph.facebook.com/102561956524622/tabs/353470634682630?access_token="+pageAccessToken,
                       function(data) {
                         console.log(data);
                     });

                     // install the app
                    var params = {};
                    params['app_id'] = '353470634682630';
                     FB.api('https://graph.facebook.com/102561956524622/tabs'+pageAccessToken, 'post', params, function(response) {
                      if (!response || response.error) {
                        console.log("Error: "+response);
                      } else {
                        console.log("Ok: "+response);
                      }
                    });
                });
              } else if (response.status === 'not_authorized') {
                console.log("the user is logged in to Facebook, but not connected to the app.");
              } else {
                console.log("the user isn't even logged in to Facebook.");
              }
             });
         };

        (function(d){
           var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = "//connect.facebook.net/en_US/all.js";
           d.getElementsByTagName('head')[0].appendChild(js);
         }(document));
      </script>
    </body>
 </html>

1 个答案:

答案 0 :(得分:1)

如果有人关心这是代码

,我设法解决了这个问题

它不起作用的一个原因是因为我使用了完整的地址“http://graph.facebook.com ...”,你可以在我使用的代码中看到 FB.api函数自动添加该地址。 另一个原因是我使用用户访问令牌来获取有关应用程序的信息,而不是页面访问令牌。 我也遇到安装应用程序的问题,因为我也需要发送页面访问令牌。

P.S:不要忘记你需要manage_pages权限。

  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '188474844587139',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true,
      });

      FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            console.log(response.authResponse);
            var userAccessToken = response.authResponse.accessToken;
            var pageId = '102561956524622';
            var appId = '353470634682630';
            console.log("User Access Token: "+userAccessToken);
            console.log("User ID: "+uid);

            // get page access token
            FB.api('/'+pageId+'?fields=access_token='+userAccessToken, function(response) {
                var pageAccessToken = response.access_token;

                // get information if user got this app
                FB.api('/'+pageId+'/tabs/'+appId+'?access_token='+pageAccessToken,
                   function(data) {
                     if (data.data.length < 1) {
                         console.log("Not installed, Installing...");

                        // install the app
                         var params = {};
                         params['app_id'] = appId;
                         FB.api('/'+pageId+'/tabs?access_token='+pageAccessToken, 'post', params, function(response) {
                             if (!response || response.error) {
                                console.log("Error Installing:");
                                console.log(response);
                             } else {
                                console.log("Installed :)");
                                console.log(response);
                             }
                        });
                     }
                     else {
                         console.log("Already installed.");
                     }
                 });
            });
          } else if (response.status === 'not_authorized') {
            console.log("the user is logged in to Facebook, but not connected to the app.");
          } else {
            console.log("the user isn't even logged in to Facebook.");
          }
         });
     };

(function(d){
   var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   d.getElementsByTagName('head')[0].appendChild(js);
 }(document));