使用Ionic/Angular应用程序发布Facebook帖子

3
我正在使用ionic框架创建一个应用程序,并集成facebookConnectPlugin以进行登录并发布到粉丝页面。
登录部分正常工作,我可以看到用户数据并在应用程序中显示。我遇到的问题是尝试在Facebook上发布文章时出现错误。
这是管理登录和发布的完整控制器。
.controller('signupCtrl', function($scope, $state, $q, UserService, $ionicLoading){
// This is the success callback from the login method
var accesstoken;
var fbLoginSuccess = function(response){
    if (!response.authResponse){
        fbLoginError("Cannot find the authResponse");
        return;
    }

    var authResponse = response.authResponse;
    accesstoken = authResponse.accessToken;

    getFacebookProfileInfo(authResponse).then(function(profileInfo){
        // For the purpose of this example I will store user data on local storage
        UserService.setUser({
            authResponse: authResponse,
            userID: profileInfo.id,
            name: profileInfo.name,
            email: profileInfo.email,
            picture: "http://graph.facebook.com/" + authResponse.userID + "/picture?type=large"
        });
        $ionicLoading.hide();
    }, function(fail){
            // Fail get profile info
            alert('profile info fail ' + fail);
        });
};

// This is the fail callback from the login method
var fbLoginError = function(error){
    alert('fbLoginError ' + error);
    $ionicLoading.hide();
};
    // This method is to get the user profile info from the facebook api
var getFacebookProfileInfo = function (authResponse){
    var info = $q.defer();

    facebookConnectPlugin.api('/me?fields=email,name&access_token=' + authResponse.accessToken, null,
        function (response) {
            console.log(response);
            info.resolve(response);
        },
        function (response) {
            console.log(response);
            info.reject(response);
        }
    );
    return info.promise;
};

//This method is executed when the user press the "Login with facebook" button
$scope.facebookSignIn = function(){
    console.log('---> facebookSignIn');
    facebookConnectPlugin.getLoginStatus(function(success){
        if (success.status === 'connected'){
            // The user is logged in and has authenticated your app, and response.authResponse supplies
            // the user's ID, a valid access token, a signed request, and the time the access token
            // and signed request each expire
            //alert('getLoginStatus ' + success.status);

            // Check if we have our user saved
            var user = UserService.getUser('facebook');

            if (!user.userID){
                alert('UNO');
                getFacebookProfileInfo(success.authResponse).then(function(profileInfo) {
                    // For the purpose of this example I will store user data on local storage
                    UserService.setUser({
                        authResponse: success.authResponse,
                        userID: profileInfo.id,
                        name: profileInfo.name,
                        email: profileInfo.email,
                        picture: "http://graph.facebook.com/" + success.authResponse.userID + "/picture?type=large"
                    });

                    //$state.go('menu.home');
                }, function(fail){
                        // Fail get profile info
                        alert('profile info fail ' + fail);
                   });
            }else{
                //alert('DOS');
                $state.go('menu.home');
                var fan_token = 'EAAH1eElPgZBl1jwZCI0BADZBlrZCbsZBWF5ig29V1Sn5ABsxH1o4kboMhpjZBDfKtD1lfDK1dJLcZBI4gRBOF2XGjOmWMXD0I8jtPZA4xLJNZADarOGx8fiXBRZCTOaxwBLQEwRjsvaqTtb2DTCI0Qdo3haX6vqHlJoWMZD';

                console.log('access token', accesstoken);

                facebookConnectPlugin.api(
                    '/186259017448757/feed', 
                    'POST', 
                    {
                        access_token: fan_token, 
                        'message':'HOLA!'
                    }, 
                    function(response){console.log(response);alert(response.id)}
                )
            }
        }else{
            // If (success.status === 'not_authorized') the user is logged in to Facebook,
            // but has not authenticated your app
            // Else the person is not logged into Facebook,
            // so we're not sure if they are logged into this app or not.

            alert('getLoginStatus ' + success.status);

            $ionicLoading.show({
                template: 'Logging in...'
            });

            // Ask the permissions you need. You can learn more about
            // FB permissions here: https://developers.facebook.com/docs/facebook-login/permissions/v2.4
            facebookConnectPlugin.login(['email', 'public_profile'], fbLoginSuccess, fbLoginError);
        }
    });
};
})

这是我尝试在Facebook墙上发布帖子的部分。

facebookConnectPlugin.api('/181297057448657/feed', 'POST', {access_token: fan_token, 'message':'HOLA!'}, 
                    function(response){
                        console.log(response);
                        alert(response.id)
                    }
                )

我去了https://developers.facebook.com/tools/explorer生成访问令牌,尝试了几个令牌(用户、粉丝页等),但似乎没有一个有效。我还使用了Facebook开发者资源管理器工具中提供的代码片段。

FB.api(
  '/174801556748297/feed',
  'POST',
  {"message":"testing3"},
  function(response) {
      // Insert your code here
  }
);

结果总是一样的,我在响应对象中得到一个JSON错误,而没有发布到Facebook墙上的帖子。
任何帮助将不胜感激!
附注:应用程序ID已被修改,这就是它们不匹配的原因。令牌和粉丝页ID也是如此。

返回的错误信息是什么? - user2085143
JSON错误,就是这样。 - Sergio
1个回答

0

我在我的应用程序中使用以下内容进行发布,参考 https://github.com/ccoenraets/sociogram-angular-ionic

 $scope.share = function () {
        OpenFB.post('/me/feed', $scope.item)
            .success(function () {
              console.log("done");
              $ionicLoading.hide();
              $scope.item.message="";
              $ionicLoading.show({ template: 'Post successfully on Facebook!', noBackdrop: true, duration: 2000 });
                $scope.status = "This item has been shared from OpenFB";
            })
            .error(function(data) {
                $ionicLoading.hide();
                alert(data.error.message);
            });
    };

HTML

 <div class="media-body"> 
      <textarea  placeholder="What's on your mind?" ng-model="item.message"></textarea> 
    </div>
    <hr style="margin-top: -1px;" />
    <div style="text-align:right;padding-right:20px;padding-bottom:10px;">
      <button class="fb-login" ng-click="share()">Post to Facebook</button>
    </div>

谢谢!我会研究一下。 - Sergio
它对我有效。如果对你也有效,请批准答案并关闭问题。谢谢。 - Anuj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接