3

我使用离子框架创建应用程序,并将facebookConnectPlugin用于登录并将帖子发布到粉丝页面。使用离子/角度应用程序制作facebook文章

登录部分工作正常,我可以看到用户数据并在应用上显示。我遇到的问题是当我尝试发布到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发布到墙上

任何帮助将非常感激!

PD:应用程序ID已被修改,为什么它们不匹配。同样的,令牌和扇页ID

+0

什么是返回的错误消息? – user2085143

+0

JSON错误,只是表示 – Sergio

回答

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> 
+0

谢谢!将调查它 – Sergio

+0

它正在为我工​​作。如果你也为你工作,然后批准答案和关闭问题。谢谢 – Anuj

相关问题