当我尝试查询Facebook时,出现以下异常: “必须使用活动访问令牌来查询有关当前用户的信息。”
这是请求:
AccessToken accessToken = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
FacebookRequestError responseError = response.getError();
if(responseError != null){
if(responseError.getCategory().equals(FacebookRequestError.Category.LOGIN_RECOVERABLE)){
LoginManager.getInstance().resolveError(MainActivity.this, response);
} else {
Toast.makeText(MainActivity.this, getString(R.string.facebookError), Toast.LENGTH_LONG).show();
Log.e(TAG, Helper.isFacebookLogin() + "");
return;
}
} else {
try {
Log.e(TAG + " newMeRequest id", object.getString("id"));
} catch (JSONException e) {
Log.e(TAG + " newMeRequest JSONerror", e.getMessage());
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,gender");
request.setParameters(parameters);
reques
t.executeAsync();
Helper.isFacebookLogin()是:
public static boolean isFacebookLogin(){
return accessToken != null && !accessToken.isExpired();
}
并返回true
。
如果返回true,为什么会显示错误消息?
答案 0 :(得分:0)
尝试使用以下代码,该代码已在多种边界条件下进行了广泛测试,并多次使用:
//on click of fb button
private void handleFBLogin() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
LoginManager.getInstance().logOut();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
LoginManager.getInstance().logInWithReadPermissions(ActivityLogin.this, Arrays.asList("public_profile", "email"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
runOnUiThread(new Runnable() {
@Override
public void run() {
setFacebookData(loginResult, d);
}
});
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Canceled login ", Toast.LENGTH_SHORT); toast.show();
}
@Override
public void onError(FacebookException exception) {
d.dismiss();
if (exception instanceof FacebookAuthorizationException) {
if (AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance().logOut();
handleFBLogin();
return;
}
}
Toast.makeText(getApplicationContext(), "ERROR " + exception.toString(), Toast.LENGTH_SHORT); toast.show();
PackageInfo info;
try {
info = getPackageManager().getPackageInfo([your package name], PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String something = new String(Base64.encode(md.digest(), 0));
//String something = new String(Base64.encodeBytes(md.digest()));
Log.e("hash key", something);
}
} catch (Exception e1) {
Log.e("name not found", e1.toString());
}
}
});
}
private void setFacebookData(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
final String firstName = response.getJSONObject().getString("first_name");
String lastName = response.getJSONObject().getString("last_name");
String id = response.getJSONObject().getString("id");
String email = null;
if (response.getJSONObject().has("email"))
email = response.getJSONObject().getString("email");
//put your code here
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
}