i am trying to implement facebook login to my app , the graph response is null and i can't get any user information , here is my code
public class LoginActivity extends AppCompatActivity {
CallbackManager callbackManager;
LoginButton login;
private String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
login = (LoginButton)findViewById(R.id.login_button);
login.setReadPermissions("public_profile email");
login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
if(AccessToken.getCurrentAccessToken() != null){
RequestData();
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
public void RequestData(){
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken()
, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
JSONObject json = response.getJSONObject();
try {
if(json != null){
text = json.getString("id");
Context context=getApplicationContext();
Toast.makeText(context,text,Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(context,"empty",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
request.executeAsync();
Context context=getApplicationContext();
Toast.makeText(context,text,Toast.LENGTH_LONG).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
the toast message is always empty and no info returning , i am stuck in this
我将注册呼叫从按钮更改为登录管理器,并且成功了
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.