2

我正在尝试实施 Facebook 登录几天。我想实现单点登录(SSO),但是当我登录到本机 Facebook 应用程序时,我收到了以下消息:

"The page you requested cannot be displayed right now. it may be temporarily unavailable, 
the link you clicked on may be broken or expired, or you  may not have perrmission to view 
this page".

我按照官方 facebook 指南登录。

LoginButton connectToFacebook = (LoginButton) findViewById(R.id.lbConnectToFacebook);
connectToFacebook.setReadPermissions(Arrays.asList("user_relationships"));
UiLifecycleHelper uiHelper = new UiLifecycleHelper(MainActivity.this, callback);
uiHelper.onCreate(savedInstanceState);

我也实现了:

private void onSessionStateChange(Session session, SessionState state, Exception   exception) {
    if (state.isOpened()) {
        Log.i("FACE", "Logged in...");
    } else if (state.isClosed()) {
        Log.i("FACE", "Logged out...");
    }
}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

并覆盖 onActivityResult 方法,但这仅在我从本机 fb 应用程序注销时才有效,并且正如我所说,我不会不使用:

connectToFacebook.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);

我想要单点登录。

4

1 回答 1

0

试试这个...

MainActivity.java

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnLogin = (Button)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {

                    @Override
                    public void call(final Session session, SessionState state, Exception exception) {
                        // TODO Auto-generated method stub
                        if(session.isOpened())
                        {
                            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                                @Override
                                public void onCompleted(GraphUser user, Response response) {
                                    // TODO Auto-generated method stub

                                    Toast.makeText(getBaseContext(), "Welcome", 1000).show();
                                                                        }
                            });
                        }

                    }
                });
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="156dp"
        android:text="@string/login" />

</RelativeLayout>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.addnewlist"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.addnewlist.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <activity android:name="com.facebook.LoginActivity" >
        </activity>
          </application>

</manifest>
于 2013-07-29T12:06:47.347 回答