0
我正在使用android-facebook-sdk,我正在嘗試請求配置文件信息。但我不能得到迴應。我使用keytool生成了密鑰哈希,並將其粘貼在Facebook應用設置中。我還在代碼中添加了應用程序ID。問題是我無法獲得個人資料信息或任何信息。Android的facebook圖形請求
public class StartingPoint extends Activity {
Facebook facebook = new Facebook("actual app id");
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
Button btnt;
/*
* On Create
* */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* button Test
* */
btnt = (Button) findViewById(R.id.btntallinn);
btnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getProfileInformation();
}
});
/*
* Get existing access_token if any
*/
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "email",
"publish_checkins", "rsvp_event", "manage_pages",
"publish_stream", "user_likes", "user_groups" },
new DialogListener() {
@Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError error) {
}
@Override
public void onError(DialogError e) {
}
@Override
public void onCancel() {
}
});
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
facebook.extendAccessTokenIfNeeded(this, null);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
// getting name of the user
final String name = profile.getString("name");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onIOException(IOException e, Object state) {
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
}
的logcat:
03-22 09:38:13.545: D/Profile(23833): {"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
03-22 09:38:13.545: W/System.err(23833): org.json.JSONException: No value for name
03-22 09:38:13.545: W/System.err(23833): at org.json.JSONObject.get(JSONObject.java:354)
03-22 09:38:13.545: W/System.err(23833): at org.json.JSONObject.getString(JSONObject.java:510)
03-22 09:38:13.545: W/System.err(23833): at com.tana.tallinn.tartu.StartingPoint$3.onComplete(StartingPoint.java:172)
03-22 09:38:13.545: W/System.err(23833): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254)