4

I'm following the tutorial for login on an Android application using facebook.

I've had many problems with the hash keys and everything and I think I've solve them all, because my session already gets the OPENED state.

The problem I'm having now is that after I log in facebook, when the session is already opened, the code executes the Request.executeMeRquestAsync() and it never enters the onComplete() part... any idea?

Here is the code...

package com.example.firstandroidapp;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.TextView;


import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity {
@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

      // callback when session changes state
      @Override
      public void call(Session session, SessionState state, Exception exception) {
          try {
                PackageInfo info = getPackageManager().getPackageInfo("com.example.firstandroidapp",
                                            PackageManager.GET_SIGNATURES);
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
                    Log.i("Digest: ", Base64.encodeToString(md.digest(), 0));
                }
            } catch (NameNotFoundException e) {
                Log.e("Test", e.getMessage());
            } catch (NoSuchAlgorithmException e) {
                Log.e("Test", e.getMessage());
            }

          if (session.isOpened()) {

          // make request to the /me API
          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
              // it never gets here...
                  if (user != null) {
                TextView welcome = (TextView) findViewById(R.id.welcome);
                welcome.setText("Hello " + user.getName() + "!");
              }
            }
          });
        }
      }
    });
  }

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

}

Thanx very much in advance. David.

4

2 回答 2

2

我有同样的问题。

教程告诉我们修改AndroidManifest.xml。在修改中,我将下面的标签放在了错误的位置(我没有使用 Eclipse 来修改清单文件)并且阻止了回调。

<manifest ...>
  ...
  <application ...>
    ...
    <activity android:name="com.facebook.LoginActivity"/>
    ...
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    ...
  </application>
  ...
  <uses-permission android:name="android.permission.INTERNET"/>
  ...
</manifest>

尝试仔细检查您的 AndroidManifest.xml。官方文档会有所帮助。

于 2013-07-28T07:08:52.263 回答
1

Session.openActiveSession如果会话已经打开,则直接返回会话。这可能是你的问题。在这种情况下可能不会调用回调。(我自己还在实施这个过程中。)

在调用它之前创建一个 SessionCallback 的实例,并且 - 如果会话直接返回 - 调用它:

StatusCallback cb = new StatusCallback() {/* implement */};
Session fbSession = Session.openActiveSession(this, true, cb);
if (fbSession != null) {
    cb.call(fbSession, fbSession.getState(), null);
}

检查回调中的会话状态,这样您就不必两次处理不同的状态。

于 2013-07-22T17:04:45.497 回答