Android - Accesstoken错误facebook

时间:2017-01-31 04:30:13

标签: java android facebook

在这里,我无法获得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。 没有获得访问令牌。 感谢。

1 个答案:

答案 0 :(得分:0)

  

要响应登录结果,您需要使用LoginManager或LoginButton注册回调。如果您使用LoginButton注册回调,则不需要在登录管理器上注册回调。

首先卸妆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

的更多信息

快乐编码。