没有使用Facebook SDK 4.10.0 Android获取用户Facebook Feed

时间:2016-02-17 14:06:20

标签: facebook facebook-graph-api android-activity android-facebook facebook-sdk-4.x

我在Android应用中使用Facebook登录。我已成功登录Facebook,但我没有得到特定用户的提要。即该用户在Facebook上发布的帖子。

我试过的就像下面......

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'依赖项。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我从Facebook找到了这些信息。我所做的就像下面一样。

当您的应用处于开发者模式时,Facebook会提供添加测试人员帐户的选项,您可以通过该帐户测试Facebook的不同功能。下面两张图片可以为您提供更好的 IDEA 。看一看。

enter image description here

enter image description here

希望这会对某人有所帮助。