在这里,我无法获取accessToken甚至个人资料图片。

不知道我在做什么错。

请帮忙 。

我把一切都做好了。

package com.example;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.*;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.facebook.login.widget.ProfilePictureView;


public class MainActivity extends AppCompatActivity {
    CallbackManager callbackManager;
    public String accessToken;
    private ProfilePictureView profilePictureView;
    String name;
    TextView fname;


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

        fname = (TextView) findViewById(R.id.textView);
        profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);

        callbackManager = CallbackManager.Factory.create();


        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile","email", "user_friends");
        // If using in a fragment
        //loginButton.setFragment(MainActivity.this);
        // Other app specific specialization

        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                accessToken = String.valueOf(loginResult.getAccessToken());
                Toast.makeText(getBaseContext(),accessToken,Toast.LENGTH_LONG).show();
                Profile profile = Profile.getCurrentProfile();
                name = profile.getFirstName();
                fname.setText(name);
                profile = Profile.getCurrentProfile();
                profilePictureView.setProfileId(profile.getId());
            }

            @Override
            public void onCancel() {
                // App code
            }

            @Override
            public void onError(FacebookException exception) {
                Toast.makeText(getBaseContext(),exception.toString(),Toast.LENGTH_LONG).show();
            }
        });

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback() {

                    @Override
                    public void onSuccess(Object o) {

                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException error) {
                        Toast.makeText(getBaseContext(),error.toString(),Toast.LENGTH_LONG).show();

                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

}


我正在尝试显示名称和dp。
没有获得访问令牌。
谢谢。

最佳答案

要响应登录结果,您需要使用LoginManager或LoginButton注册回调。如果使用LoginButton注册回调,则无需在Login Manager上注册回调。


因此,首先卸妆LoginManager

现在,您可以使用GraphRequest获取用户数据,这是您可以执行的操作。

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {

        final ProgressDialog pd = new ProgressDialog(Login.this);
        pd.setMessage("Please Wait..");
        String accessToken = loginResult.getAccessToken().getToken();
        Log.i("accessToken", accessToken);

        GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object,
                                        GraphResponse response) {
                    Log.i("LoginActivity",
                        response.toString());
                    try {
                        id = object.getString("id");
                        try {
                            URL profile_pic = new URL(
                                                    "https://graph.facebook.com/" + id + "/picture?type=large");
                            user_image = profile_pic.toString();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }

                        name = object.getString("name");
                        email = object.getString("email");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        Bundle parameters = new Bundle();
        parameters.putString("fields",
                "id,name,email,gender, birthday");
        request.setParameters(parameters);
        request.executeAsync();
        pd.show();
    }

    @Override
    public void onCancel() {
        // App code
    }

    @Override
    public void onError(FacebookException exception) {
        Toast.makeText(getBaseContext(),exception.toString(),Toast.LENGTH_LONG).show();
    }
});


了解更多有关GraphRequest Graph API的信息

编码愉快。

10-08 11:09