facebook sdk:“必须使用活动访问令牌来查询有关当前用户的信息”
Posted
技术标签:
【中文标题】facebook sdk:“必须使用活动访问令牌来查询有关当前用户的信息”【英文标题】:facebook sdk: "An active access token must be used to query information about the current user" 【发布时间】:2018-10-31 18:54:37 【问题描述】:当我尝试查询 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,为什么我会收到错误消息?
【问题讨论】:
【参考方案1】:尝试使用以下代码,此代码已在多个边界条件下进行了广泛测试并多次使用:
//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();
【讨论】:
这篇文章对我没有任何帮助。我仍然收到相同的错误消息。以上是关于facebook sdk:“必须使用活动访问令牌来查询有关当前用户的信息”的主要内容,如果未能解决你的问题,请参考以下文章
facebook Uncaught OAuthException:必须使用活动访问令牌来查询有关当前用户的信息
OAuthException using Socialite with facebook - "message": "必须使用活动访问令牌来查询有关当前用户的信息。&qu
OOAuthException:必须使用活动访问令牌来查询有关当前用户的信息