无法通过 Android 应用程序访问 Facebook 好友

发布于 2024-12-16 15:04:18 字数 4308 浏览 7 评论 0原文

我在通过 Android 应用程序访问图形 API 时遇到问题,该应用程序以 JSONObject 形式检索用户的朋友,提取他们的姓名并将其显示在屏幕上。这应该是一个简单直接的任务,但显然事实并非如此。当我在 Android Nexus I 上运行应用程序时,我登录 Facebook,然后系统要求我单击“允许”按钮授予权限,然后我被重定向到空白页面。我希望看到我朋友的名字,但它没有发生。有人可以帮助我吗?

public class Login extends Activity 
{
    Facebook mFacebook = new Facebook("201509899926116");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
    View linearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        linearLayout = findViewById(R.id.main_layout);

         /* Get existing access_token if any */
        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if(access_token != null)
        {
            mFacebook.setAccessToken(access_token);
        }
        if(expires != 0)
        {
            mFacebook.setAccessExpires(expires);
        }

        /* Only call authorize if the access_token has expired. */
        if(!mFacebook.isSessionValid())
        {
            mFacebook.authorize(this, new String[] {"user_birthday","email",
                  "user_relationships","user_religion_politics","user_hometown",
                  "user_location","user_relationship_details","user_education_history",
                  "user_likes","user_interests", "user_activities"},
                  new DialogListener() 
            {

                @Override
                public void onComplete(Bundle values)
                {
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", mFacebook.getAccessToken());
                    editor.putLong("access_expires", mFacebook.getAccessExpires());
                    editor.commit();

                    /* access the graph API */
                    Log.d("Facebook-Example-Friends Request", "Started API request");
                    mAsyncRunner.request("me/friends", new FriendsRequestListener());
                    Log.d("Facebook-Example-Friends Request", "Finished API request");
                }

                @Override
                public void onFacebookError(FacebookError error) {}

                @Override
                public void onError(DialogError e) {}

                @Override
                public void onCancel() {}
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        mFacebook.authorizeCallback(requestCode, resultCode, data);
    }

    public class FriendsRequestListener implements com.facebook.android.AsyncFacebookRunner.RequestListener 
    {
        /**
         * Called when the request to get friends has been completed.
         * Retrieve and parse and display the JSON stream.
         */
        public void onComplete(final String response)
        {
            try 
            {
                // process the response here: executed in background thread
                Log.d("Facebook-Example-Friends Request", "response.length(): " + response.length());
                Log.d("Facebook-Example-Friends Request", "Response: " + response);

                final JSONObject json = new JSONObject(response);
                JSONArray d = json.getJSONArray("data");
                int l = (d != null ? d.length() : 0);
                Log.d("Facebook-Example-Friends Request", "d.length(): " + l);

                for (int i=0; i<l; i++) 
                {
                    JSONObject o = d.getJSONObject(i);
                    String n = o.getString("name");
                    String id = o.getString("id");

                    TextView tv = new TextView(Login.this);
                    tv.setText(n);
                    ((LinearLayout) linearLayout).addView(tv);
                }               
            }
            catch (JSONException e)
            {
                Log.w("Facebook-Example", "JSON Error in response");
            }
        }
    }
}

I have a problem with accessing the graph API through my android application that retrieves a user's friends as JSONObject, extracts their names and displays them on the screen. It is supposed to be a simple and straightforward task but apparently it is not. When I run my application on Android Nexus I, I login to Facebook, then I am asked I click "Allow" button to grant permissions and I am redirected to a blank page. I am expect to see the name of my friends but it does not happen. Could some one assist me.

public class Login extends Activity 
{
    Facebook mFacebook = new Facebook("201509899926116");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
    View linearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        linearLayout = findViewById(R.id.main_layout);

         /* Get existing access_token if any */
        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if(access_token != null)
        {
            mFacebook.setAccessToken(access_token);
        }
        if(expires != 0)
        {
            mFacebook.setAccessExpires(expires);
        }

        /* Only call authorize if the access_token has expired. */
        if(!mFacebook.isSessionValid())
        {
            mFacebook.authorize(this, new String[] {"user_birthday","email",
                  "user_relationships","user_religion_politics","user_hometown",
                  "user_location","user_relationship_details","user_education_history",
                  "user_likes","user_interests", "user_activities"},
                  new DialogListener() 
            {

                @Override
                public void onComplete(Bundle values)
                {
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", mFacebook.getAccessToken());
                    editor.putLong("access_expires", mFacebook.getAccessExpires());
                    editor.commit();

                    /* access the graph API */
                    Log.d("Facebook-Example-Friends Request", "Started API request");
                    mAsyncRunner.request("me/friends", new FriendsRequestListener());
                    Log.d("Facebook-Example-Friends Request", "Finished API request");
                }

                @Override
                public void onFacebookError(FacebookError error) {}

                @Override
                public void onError(DialogError e) {}

                @Override
                public void onCancel() {}
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        mFacebook.authorizeCallback(requestCode, resultCode, data);
    }

    public class FriendsRequestListener implements com.facebook.android.AsyncFacebookRunner.RequestListener 
    {
        /**
         * Called when the request to get friends has been completed.
         * Retrieve and parse and display the JSON stream.
         */
        public void onComplete(final String response)
        {
            try 
            {
                // process the response here: executed in background thread
                Log.d("Facebook-Example-Friends Request", "response.length(): " + response.length());
                Log.d("Facebook-Example-Friends Request", "Response: " + response);

                final JSONObject json = new JSONObject(response);
                JSONArray d = json.getJSONArray("data");
                int l = (d != null ? d.length() : 0);
                Log.d("Facebook-Example-Friends Request", "d.length(): " + l);

                for (int i=0; i<l; i++) 
                {
                    JSONObject o = d.getJSONObject(i);
                    String n = o.getString("name");
                    String id = o.getString("id");

                    TextView tv = new TextView(Login.this);
                    tv.setText(n);
                    ((LinearLayout) linearLayout).addView(tv);
                }               
            }
            catch (JSONException e)
            {
                Log.w("Facebook-Example", "JSON Error in response");
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-12-23 15:04:18

我想我可能已经发现了这个问题..根据 onComplet 的文档;

     /**
     * Called when a request completes with the given response.
     * 
     * Executed by a background thread: do not update the UI in this method.
     */

而且您正在尝试更新您的 UI,因此 addViews 实际上不会反映在 UI 线程上。您应该在活动中创建一个方法,您可以使用 runOnUIThread 从 onComplete 调用该方法。像这样;

runOnUiThread(new Runnable() {
            public void run() {
                inserFacebookNames(facebookContactNames);
            }
        });

让我知道这是否真的解决了它..我很好奇这是否是原因..

I think i might've found the issue.. According to the documentation on onComplet;

     /**
     * Called when a request completes with the given response.
     * 
     * Executed by a background thread: do not update the UI in this method.
     */

And you are trying to update your UI, so addViews don't actually get reflected on the UI thread.. You should create a method in your activity that you can call from onComplete with runOnUIThread.. Something like this;

runOnUiThread(new Runnable() {
            public void run() {
                inserFacebookNames(facebookContactNames);
            }
        });

Let me know if this actually fixes it.. I'm curious if that was the reason..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文