0

我在我的Android應用程序中使用Facebook登錄。我已成功登錄Facebook,但沒有收到特定的用戶信息。即該用戶在Facebook上發佈的帖子。使用Facebook SDK獲取用戶Facebook供稿4.10.0 Android

我已經試過像下面...

package com.cloudant_db_demo.android02.insightnewapp; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 

import com.facebook.AccessToken; 
import com.facebook.CallbackManager; 
import com.facebook.FacebookCallback; 
import com.facebook.FacebookException; 
import com.facebook.FacebookSdk; 
import com.facebook.GraphRequest; 
import com.facebook.GraphResponse; 
import com.facebook.HttpMethod; 
import com.facebook.login.LoginManager; 
import com.facebook.login.LoginResult; 
import com.facebook.login.widget.LoginButton; 
import com.ibm.watson.developer_cloud.personality_insights.v2.PersonalityInsights; 
import com.ibm.watson.developer_cloud.personality_insights.v2.model.Profile; 
import com.twitter.sdk.android.Twitter; 
import com.twitter.sdk.android.core.Callback; 
import com.twitter.sdk.android.core.Result; 
import com.twitter.sdk.android.core.TwitterAuthConfig; 
import com.twitter.sdk.android.core.TwitterCore; 
import com.twitter.sdk.android.core.TwitterException; 
import com.twitter.sdk.android.core.TwitterSession; 
import com.twitter.sdk.android.core.identity.TwitterLoginButton; 
import com.twitter.sdk.android.core.models.Tweet; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

import io.fabric.sdk.android.Fabric; 

public class MainActivity extends AppCompatActivity { 

    // Note: Your consumer key and secret should be obfuscated in your source code before shipping. 
    private static final String TWITTER_KEY = "twitter_key"; 
    private static final String TWITTER_SECRET = "twitter_secret"; 

    CallbackManager callbackManager; 
    private TwitterLoginButton twitterLoginButton; 
    TwitterSession twitterSession; 

    LoginButton fb_login_button; 
    AccessToken mAccessToken; 

    public static String social_email = "", social_id = "", social_feed = "", social_name = "", social = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); 
     Fabric.with(this, new Twitter(authConfig)); 
     setContentView(R.layout.activity_main); 

     List<String> permissions = new ArrayList<String>(); 
     permissions.add("user_posts"); 

     fb_login_button = (LoginButton) findViewById(R.id.login_button); 
     fb_login_button.setReadPermissions(permissions); 

     //Facebook CallBack 
     callbackManager = CallbackManager.Factory.create(); 
     LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_status", "user_posts")); 
     LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       mAccessToken = loginResult.getAccessToken(); 
       social_id = mAccessToken.getUserId(); 
       social = "fb"; 

       Bundle params = new Bundle(); 
       params.putString("fields", "user_posts"); 

       /* make the API call for Feed */ 
       new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + social_id + "/feed/", null, HttpMethod.GET, 
         new GraphRequest.Callback() { 
          public void onCompleted(GraphResponse response) { 
           Log.e("Fb Feed: ", response.toString()); 
           social_feed = social_feed + " " + response.toString() + " "; 
           Thread thread = new Thread(new Runnable() { 
            @Override 
            public void run() { 
             callWatsonAPI(social); 
            } 
           }); 
           thread.start(); 
          } 
         }).executeAsync(); 
      } 

      @Override 
      public void onCancel() { 
      } 

      @Override 
      public void onError(FacebookException exception) { 
      } 
     }); 

     //Twitter CallBack 
     twitterLoginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button); 
     twitterLoginButton.setCallback(new Callback<TwitterSession>() { 
      @Override 
      public void success(Result<TwitterSession> result) { 
       twitterSession = result.data; 
       social_id = Long.toString(twitterSession.getUserId()); 
       social_name = twitterSession.getUserName(); 
       getUserTweets(); 
      } 

      @Override 
      public void failure(TwitterException e) { 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     twitterLoginButton.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
    } 

    void getUserTweets() { 
     TwitterCore.getInstance().getApiClient(twitterSession).getStatusesService() 
       .userTimeline(null, 
         social_name, 
         null, 
         null, 
         null, 
         null, 
         null, 
         null, 
         null, 
         new Callback<List<Tweet>>() { 
          @Override 
          public void success(final Result<List<Tweet>> result) { 
           new Thread(new Runnable() { 
            @Override 

            public void run() { 
             try { 
              for (Tweet t : result.data) { 
               social_feed = social_feed + " " + t.text.toString() + " "; 
              } 

              Thread thread = new Thread(new Runnable() { 
               @Override 
               public void run() { 
                callWatsonAPI(social); 
               } 
              }); 
              thread.start(); 
             } catch (Exception ex) { 
              ex.printStackTrace(); 
             } 
            } 
           }).start(); 
          } 

          @Override 
          public void failure(TwitterException exception) { 
           android.util.Log.d("twittercommunity", "exception " + exception); 
          } 
         }); 
    } 

    void callWatsonAPI(String social) { 
     PersonalityInsights service = new PersonalityInsights(); 
     service.setUsernameAndPassword("27324e40-6c74-44f0-a3b9-9659cf5b4ed5", "eGXEQ8EGpVTl"); 
     service.setEndPoint("https://gateway.watsonplatform.net/personality-insights/api"); 
     Profile profile = service.getProfile(social_feed); 

     /*Log.e("Watson Response: ", "" + profile);*/ 

     if (social.equals("fb")) { 
      Intent intent = new Intent(getApplicationContext(), SecondActivity.class); 
      intent.putExtra("watson_res", profile.toString()); 
      intent.putExtra("Fb Feed", social_feed); 
      startActivity(intent); 
     } else { 
      Intent intent = new Intent(getApplicationContext(), SecondActivity.class); 
      intent.putExtra("watson_res", profile.toString()); 
      intent.putExtra("Twitter Feed", social_feed); 
      startActivity(intent); 
     } 
    } 
} 

我得到一個特定的Facebook帳戶無效JSON響應。在這個帳戶中,我已經爲公衆設置了安全。但對於我的個人Facebook帳戶,我得到{"data":[]}。即無迴應。

Invalid JSON Facebook response for particular Facebook account

Null Data JSON Facebook response for My Personal Facebook account

我已經在的build.gradle文件相關使用compile 'com.facebook.android:facebook-android-sdk:4.10.0'

在此先感謝。

+0

你問用戶有什麼權限? – CBroe

+0

我還沒有向用戶詢問任何權限。@ CBroe –

+0

https://developers.facebook.com/docs/graph-api/reference/v2.5/user/feed#read – CBroe

回答

0

我已經找到來自Facebook的資訊提供。我所做的就像下面。

當您的應用程序處於開發者模式時Facebook提供選項來添加測試人員帳戶,通過它您可以測試Facebook的不同功能。下面兩張圖片可以給你更好的關於它的IDEA。看看那個。

enter image description here

enter image description here

希望這將幫助別人。

相關問題