无法使用 Facebook SDK 4.10.0 获取用户 Facebook 提要 Android

Not getting user Facebook feed using Facebook SDK 4.10.0 Android

我在 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 响应。在该帐户中,我已将安全设置为 public。但是对于我的个人 Facebook 帐户,我得到 {"data":[]}。即响应为空。

我在 build.gradle 文件的依赖项中使用了 compile 'com.facebook.android:facebook-android-sdk:4.10.0'

提前致谢。

我找到了来自 Facebook 的提要。我所做的如下所示。

当您的应用程序处于开发者模式时,Facebook 会提供添加 Testers 帐户的选项,您可以通过这些帐户测试 Facebook 的不同功能。下面两张图片可以给你更好的IDEA。看看那个。

希望这对某人有所帮助。