0

我正在尝试连接 facebook 并在 facebook 中获取我的详细信息,但是当我想获取我的姓名时,我得到一个 JSONException 错误:“”必须使用活动访问令牌来查询有关当前用户的信息“

我标记了它发生的那一行

多谢

这是我的课:

public class MainActivity extends Activity {

    private Button logInButton;
    private TextView registerScreen;
    private TextView userText;
    private TextView passwordText;
    private SharedPreferences loginPreferences;
    private SharedPreferences.Editor loginPrefsEditor;
    private Boolean saveLogin;
    private CheckBox saveLoginCheckBox;
    private Button buttonLoginLogout;
    private ShareActionProvider provider;
    private TextView forgotPassword;
    private String userName="";


    private String friendsId="";
    private String friendsName="";
    private List<String> freindsIdList =new ArrayList<String>();

    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    private static String APP_ID = "xxxxxx" ;


    public void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.login);

        facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);

        final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

        buttonLoginLogout=(Button) findViewById(R.id.facebooklog);

        buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    loginToFacebook();
                    getProfileInformation();
                }
        });

    public void loginToFacebook() {
        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            facebook.setAccessToken(access_token);
        }

        if (expires != 0) {
            facebook.setAccessExpires(expires);
        }

        if (!facebook.isSessionValid()) {
            facebook.authorize(this,
                    new String[] { "email", "publish_stream" },
                    new DialogListener() {

                        @Override
                        public void onCancel() {
                            // Function to handle cancel event
                        }

                        @Override
                        public void onComplete(Bundle values) {
                            // Function to handle complete event
                            // Edit Preferences and update facebook acess_token
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token",
                                    facebook.getAccessToken());
                            editor.putLong("access_expires",
                                    facebook.getAccessExpires());
                            editor.commit();
                        }

                        @Override
                        public void onError(DialogError error) {
                            // Function to handle error

                        }

                        @Override
                        public void onFacebookError(FacebookError fberror) {
                            // Function to handle Facebook errors

                        }

                    });
        }
    }




    public void getProfileInformation() {
        mAsyncRunner.request("me", new RequestListener() {
            @Override
            public void onComplete(String response, Object state) {
                Log.d("Profile", response);
                String json = response;
                try {
                    JSONObject profile = new JSONObject(json);
                    // getting name of the user
                    final String name = profile.getString("name");       //this line i get an JSONException with error: 
                                                                        //"An active access token must be used to query information about the current user "
                    // getting email of the user
                    final String email = profile.getString("email");

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
                        }

                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onIOException(IOException e, Object state) {
            }

            @Override
            public void onFileNotFoundException(FileNotFoundException e,
                    Object state) {
            }

            @Override
            public void onMalformedURLException(MalformedURLException e,
                    Object state) {
            }

            @Override
            public void onFacebookError(FacebookError e, Object state) {
            }
        });
    }

}

这是logcat:

03-18 12:24:32.081: W/System.err(858): org.json.JSONException: No value for name
03-18 12:24:32.131: W/System.err(858):  at org.json.JSONObject.get(JSONObject.java:354)
03-18 12:24:32.131: W/System.err(858):  at org.json.JSONObject.getString(JSONObject.java:510)
03-18 12:24:32.150: W/System.err(858):  at com.example.sale.MainActivity$3.onComplete(MainActivity.java:291)
03-18 12:24:32.150: W/System.err(858):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:276)
4

1 回答 1

2

我推荐使用 facebooks 新的 Android 3.0 Facebook 库。它有一个很好的教程和预建的类来管理状态和做查询。它比以前的库更简单、更干净

https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

查看等到获取我的 id 和我的朋友 id facebook 以获取更多信息

于 2013-03-18T12:29:45.460 回答