Facebook に接続して Facebook で自分の詳細情報を取得しようとしていますが、自分の名前を取得しようとすると、「現在のユーザーに関する情報を照会するにはアクティブなアクセス トークンを使用する必要があります」というエラーで JSONException が発生します。
私はそれが起こっている行をマークしました
どうもありがとう
これは私のクラスです:
public class MainActivity extends Activity {
private Button logInButton;
private TextView registerScreen;
private TextView userText;
private TextView passwordText;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
private CheckBox saveLoginCheckBox;
private Button buttonLoginLogout;
private ShareActionProvider provider;
private TextView forgotPassword;
private String userName="";
private String friendsId="";
private String friendsName="";
private List<String> freindsIdList =new ArrayList<String>();
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
private static String APP_ID = "xxxxxx" ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
buttonLoginLogout=(Button) findViewById(R.id.facebooklog);
buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginToFacebook();
getProfileInformation();
}
});
public void loginToFacebook() {
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);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
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"); //this line i get an JSONException with error:
//"An active access token must be used to query information about the current user "
// 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-18 12:24:32.081: W/System.err(858): org.json.JSONException: No value for name
03-18 12:24:32.131: W/System.err(858): at org.json.JSONObject.get(JSONObject.java:354)
03-18 12:24:32.131: W/System.err(858): at org.json.JSONObject.getString(JSONObject.java:510)
03-18 12:24:32.150: W/System.err(858): at com.example.sale.MainActivity$3.onComplete(MainActivity.java:291)
03-18 12:24:32.150: W/System.err(858): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:276)