1

我在我的应用程序中使用以下代码通过 Facebook 登录。我需要使用 Facebook SSO 执行此操作。我有正确的 app_id。

package com.fb.sso;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class FBSSOActivity extends Activity {
    /** Called when the activity is first created. */


    public static final String APP_ID = "my_app_id";

    private static final String[] PERMISSIONS = new String[] {
            "publish_stream", "read_stream", "offline_access" };
    private TextView mText;
    private Handler mHandler = new Handler();

    private Facebook mFacebook;
    private AsyncFacebookRunner mAsyncRunner;
    byte[] raw;
    private SharedPreferences mPrefs;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (APP_ID == null) {
            Util.showAlert(this, "Warning",
                    "Facebook Applicaton ID must be set...");
        }

        // Initialize the content view
        setContentView(R.layout.main);

        // Initialize the Facebook session
        mFacebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(mFacebook);

        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);
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("FB Sample App", "onActivityResult(): " + requestCode);
        mFacebook.authorizeCallback(requestCode, resultCode, data);
    }



    private class LogoutRequestListener implements RequestListener {

        @Override
        public void onComplete(String response, Object state) {
            // TODO Auto-generated method stub

Log.v("comes here>>.","sucess");
            // Only the original owner thread can touch its views
            FBSSOActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    mText.setText("Thanks for using FB Sample App. Bye bye...");

                }
            });

            // Dispatch on its own thread
            mHandler.post(new Runnable() {
                public void run() {
                }
            });

        }

        @Override
        public void onIOException(IOException e, Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
            // TODO Auto-generated method stub
            Log.v("facebook error","fb error");
        }
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        MenuItem loginItem = menu.findItem(R.id.login);

        if (mFacebook.isSessionValid()) {
            loginItem.setTitle("Logout");


        } else {
            loginItem.setTitle("Login");


        }
        loginItem.setEnabled(true);
        return super.onPrepareOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        // Login/logout toggle
        case R.id.login:

            if (!mFacebook.isSessionValid()) {

                mFacebook.authorize(this, new DialogListener() {

                    @Override
                    public void onComplete(Bundle values) {
                        // TODO Auto-generated method stub

                        Log.v("Entered ", "No ERRRRRRRRRRRR");
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                mFacebook.getAccessToken());
                        editor.putLong("access_expires",
                                mFacebook.getAccessExpires());
                        editor.commit();
                        Intent i=new Intent(FBSSOActivity.this,second.class);
                        startActivity(i);

                    }

                    @Override
                    public void onFacebookError(FacebookError e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onError(DialogError e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub

                    }

                });
            }else{

                mFacebook.setAccessToken(null);
                mFacebook.setAccessExpires(0);

                AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook);
                 asyncRunner.logout(this.getBaseContext(), new  LogoutRequestListener());
            }

            break;


        default:
            return false;

        }
        return true;
    }

    }

我在我的设备中登录了预装的 Facebook 应用程序。我只有这个屏幕。 在此处输入图像描述

在我的 Logcat 中出现了这一行。

09-28 15:18:24.652: E/ActivityThread(1201): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider

如果我从预装的 Facebook 应用程序中注销,我的应用程序会提示我登录并询问凭据。那个时候也出现了空白屏幕。但是在预装的 Facebook 应用程序中,我可以看到我的更新。(我登录了)。

更新:

现在我只检查应用程序。我还没有将它发布到市场上。我通过参考这个网站http://sean.lyn.ch/2011/07/android-the-facebook-sdk-sso-and-you/得到了钥匙。我将它添加到应用程序中,就像这样。 在此处输入图像描述

更新 2:现在我在 logcat 中得到了这些行:

09-29 12:00:12.552: I/ActivityManager(73): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) }
09-29 12:00:13.022: I/ActivityManager(73): Displayed activity com.facebook.katana/.ProxyAuth: 436 ms (total 436 ms)
09-29 12:00:15.032: W/InputManagerService(73): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@434b2e88 (uid=10031 pid=2233)
09-29 12:00:15.032: W/IInputConnectionWrapper(2233): showStatusIcon on inactive InputConnection
4

1 回答 1

-2

这可能是由于以下原因造成的:

  1. 您没有连接到互联网
  2. 您没有授予 Internet 访问权限 (Manifest.xml)
  3. 您没有为应用程序使用正确的哈希键
  4. 您没有提供正确的 App Id
于 2012-11-21T11:45:35.060 回答