如果(session.isOpen()),在Android上的Facebook登录总是返回falseisOpen、session、Android、false

2023-09-04 06:28:29 作者:她妈为毛不喜欢我

我想实现我的Andr​​oid应用程序中的简单的活动,其中一个要求用户通过Facebook登录,使用户的喜欢被检索。所以,很显然首先我通过Facebook测试一个简单的登录。我使用的是精确的code,他们对入门Facebook的SDK为Android - 第6步(https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/)但由于某种原因它不工作当我调试的code,我意识到以下条件:

I'm trying to implement a simple activity in my android application where a user is asked to login via facebook so that the user's 'likes' are retrieved. So obviously first I'm testing a simple login via facebook. I'm using the exact code they have on 'Getting Started with the Facebook SDK for Android' - Step 6 (https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/) but for some reason it's not working. When I debugged the code I realised that the following condition:

if(session.isOpen()) 

是始终返回false,即使我已经登录到Facebook上。我已经竭尽所能来解决它​​,但似乎没有奏效。

is ALWAYS returning false even though I'd already be logged onto facebook. I've tried everything to fix it but nothing seems to be working.

任何人有这个问题,或者知道如何解决它?

Anybody had this issue or knows how to fix it?

编辑:我想这个测试我的手机不能直接在模拟器上。我不知道这是否可能是一个问题。

I'm trying this test on my phone directly not on an emulator. I don't know if that could be an issue.

在此先感谢!

和我也尝试这种解决方案session.isOpened()返回false即使成功登录到Facebook 但使用Java 6或7还给出了相同的散列键。

And I did try this solution session.isOpened() returns false even if successfully logged in to Facebook but using java 6 or 7 still gave the same hash key.

编辑2:我也尝试过同样的事情,但略有不同的方法。 http://sonyarouje.com/2011/09/18/facebook-hash-key-for-android-apps/我仍然有同样的问题。在 session.isOpen()的方法总是返回false。

EDIT 2: I've also tried the same thing but with a slightly different approach. http://sonyarouje.com/2011/09/18/facebook-hash-key-for-android-apps/ I'm still having the same problem. The session.isOpen() method is always returning false.

修改3:以下是最新的code我试过。我不认为有在logcat中的任何错误。无论如何,我会在这里链接,以防万一我失去了一些东西。

EDIT 3: Here is the latest code I've tried. I don't think there are any errors in the logcat. Anyway I'll link it here just in case I'm missing something.

package com.example.danandroidapp;
import java.util.Arrays;

import com.facebook.FacebookException;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.LoginButton.OnErrorListener;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private String TAG = "MainActivity";
    private TextView lblEmail;

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

        lblEmail = (TextView) findViewById(R.id.lblEmail);

        LoginButton authButton = (LoginButton) findViewById(R.id.authButton);

        authButton.setOnErrorListener(new OnErrorListener() {
            @Override
            public void onError(FacebookException error) {
                Log.i(TAG, "Error " + error.getMessage());
            }
        });

        authButton.setReadPermissions(Arrays.asList("basic_info", "email"));
        authButton.setSessionStatusCallback(new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if(session.isOpened()) {
                Log.i(TAG, "Access Token " + session.getAccessToken());
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if(user != null) {
                            Log.i(TAG, "User ID " + user.getId());
                            Log.i(TAG, "Email " + user.asMap().get("email"));
                            lblEmail.setText(user.asMap().get("email").toString());
                        }
                    }
                });
            }
        }
    });
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

和我logcat的输出:https://www.dropbox.com/s/7qg9zbhlpikfovf/log.txt

And my logcat output: https://www.dropbox.com/s/7qg9zbhlpikfovf/log.txt

推荐答案

我有类似的问题,这是hashkey错在Facebook上,遵循以下code就可以得到已经发送到哈希键Facebook上。只需复制此hashkey并更换。这将开始工作。

I had the similar problem it was the hashkey was wrong in facebook, by following the below code you can get the hash key that has been sent to facebook. Just copy this hashkey and replace it. It will start working.

    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "your.root.package", 
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }