android - Facebook 访问 token 始终为空

标签 android facebook

如果 AccessToken.getCurrentAccessToken() 为空,我的主 Activity 中有一些代码调用我的 facebook 登录 Activity 类:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import java.util.Arrays;

import matthewboyle.lurker_android.MainFeedScreen;
import matthewboyle.lurker_android.utilities.ConnectionChecker;

/**
 * Created by matthewboyle on 28/05/2017.
 */

public class FacebookLogin extends AppCompatActivity {
    private CallbackManager mCallbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCallbackManager = CallbackManager.Factory.create();
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                Log.d("facebook", "onCurrentAccessTokenChanged");


            }
        };
        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                Log.d("facebook", "onCurrentProfileChanged");

            }
        };
        accessTokenTracker.startTracking();
        profileTracker.startTracking();

        LoginManager.getInstance().registerCallback(mCallbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        Log.d("facebook", "in on success");
                        AccessToken accessToken = loginResult.getAccessToken();
                        Log.d("permissions", accessToken.getPermissions().toString());

                        Log.d("facebook", "in on success,got a token and its "+accessToken);

                    }

                    @Override
                    public void onCancel() {
                        Log.d("facebook", "in cancel");

                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Log.d("facebook", "in error");
                        Log.d("facebook", exception.toString());




                    }


                });
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_posts","user_likes","user_about_me","user_managed_groups","user_tagged_places"));
        Log.d("facebook", "Done so redirecting with "+AccessToken.getCurrentAccessToken());

        startActivity(new Intent(FacebookLogin.this,MainFeedScreen.class));


    }
    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        super.onActivityResult(requestCode, responseCode, intent);
        //Facebook login
        mCallbackManager.onActivityResult(requestCode, responseCode, intent);

    }
}

如果我运行这段代码,我得到的唯一打印语句是:

 Done so redirecting with null

它似乎根本没有命中类中的任何其他方法。

值得一提的是,在我重新安装应用程序之前,这段代码工作正常。我去了 Facebook 并更新了我应用程序中的哈希键,但这似乎没有帮助。但是,由于哈希键,我没有看到任何错误。

非常感谢任何帮助。

最佳答案

您需要像这样在“onSuccess”回调中移动“startActivity”行:

LoginManager.getInstance().registerCallback(mCallbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d("facebook", "in on success");
                    AccessToken accessToken = loginResult.getAccessToken();
                    Log.d("permissions", accessToken.getPermissions().toString());

                    Log.d("facebook", "in on success,got a token and its "+accessToken);

                    Log.d("facebook", "Done so redirecting with "+AccessToken.getCurrentAccessToken());

                    startActivity(new Intent(FacebookLogin.this,MainFeedScreen.class));

                }

                @Override
                public void onCancel() {
                    Log.d("facebook", "in cancel");

                }

                @Override
                public void onError(FacebookException exception) {
                    Log.d("facebook", "in error");
                    Log.d("facebook", exception.toString());




                }


            });

关于android - Facebook 访问 token 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45064637/

相关文章:

android - Xamarin 资源目录 ..\.\res 不存在

android - 如何水平旋转由 drawTextonPath 创建的圆形文本?

android - 如何正确处理嵌套 fragment ?

android - 带有多个按钮的通知

android - Facebook 登录对话框覆盖覆盖 Activity

php - 用于 facebook/heroku 的 AS3 postgresql 和 amfphp?

javascript - 更改 Facebook 点赞按钮设计

覆盆子上的 Android things - 从 firebase 发送推送通知

php - Facebook PHP 抛出异常 "(#803) Some of the aliases you requested do not exist"

iphone - Facebook iOS SDK : session invalid on iPhone after logging out of Facebook from a desktop