Facebookページの情報を取得しようとしていますが、アプリを初めてインストールして実行すると、アプリが表示されます。
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
次回アプリケーションを実行すると、正しい結果が返されます。なぜこれが発生するのですか。インストール後にアプリを初めて実行したときにデータを返すようにします。
これが私のFacebookのSDKコードです:
private String access_Token="";
private final String APP_ID="MY_APP_ID";
private final String[] PERMS = new String[] { "publish_stream","manage_pages" };
private Bundle params=new Bundle();
private SharedPreferences sharedPrefs;
private AsyncFacebookRunner mAsyncRunner;
private Facebook mfacebook;
private TextView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=(TextView)findViewById(R.id.Mozi);
this.SetConnection(); //Initialize Fb
this.getAccessToken(); //GetAccessToken
this.CheckSessionExpiry(); //Create Session with permissions if expired
this.RetrieveUserPages();
// this.EnableFBLogout();
}
public void onResume() {
super.onResume();
mfacebook.extendAccessTokenIfNeeded(this, null);
}
private void EnableFBLogout()
{
mAsyncRunner.logout(getApplicationContext(), new RequestListener() {
@Override
public void onComplete(String response, Object state) {
String method = "DELETE";
Bundle params = new Bundle();
/*
* this will revoke 'publish_stream' permission
* Note: If you don't specify a permission then this will de-authorize the application completely.
*/
params.putString("permission", "publish_stream");
mAsyncRunner.request("/me/permissions", params, method,new RequestListener() {
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
Log.e("PerMalform",e.getMessage());
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
Log.e("PerMalform",e.getMessage());
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
Log.e("PerMalform",e.getMessage());
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
Log.e("PerMalform",e.getMessage());
}
@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.e("PerMalform",response);
}
}, null);
}
@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) {}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
this.mfacebook.authorizeCallback(requestCode, resultCode, data);
}
private void getAccessToken()
{
sharedPrefs= getPreferences(MODE_PRIVATE);
String access_token = sharedPrefs.getString("access_token", null);
long expires = sharedPrefs.getLong("access_expires", 0);
if(access_token != null) {
mfacebook.setAccessToken(access_token);
}
if(expires != 0) {
mfacebook.setAccessExpires(expires);
}
}
private void CheckSessionExpiry()
{
if(!mfacebook.isSessionValid()) {
mfacebook.authorize(this, this.PERMS , new DialogListener() {
@Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("access_token", mfacebook.getAccessToken());
editor.putLong("access_expires", mfacebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError error) {
Log.e("mozi1",error.toString());
}
@Override
public void onError(DialogError e) {
Log.e("mozi2",e.toString());
}
@Override
public void onCancel() {
Log.e("sad","ww");
}
});
}
}
private void SetConnection()
{
this.mfacebook=new Facebook(this.APP_ID);
this.mAsyncRunner=new AsyncFacebookRunner(mfacebook);
}
private void RetrieveUserPages()
{
this.params.putString(Facebook.TOKEN, mfacebook.getAccessToken());
this.mAsyncRunner.request("me/accounts", this.params, "GET", new RequestListener() {
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
Log.e("Malformed",e.getMessage());
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
Log.e("IO",e.getMessage());
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
Log.e("FNF",e.getMessage());
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
Log.e("FBERR",e.getMessage());
}
@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.i("responsefromFB",response); //here the response is an error for the first time and data the second time.
// view.setText(response);
}
}, null);
}