Facebookのユーザープロフィール情報を取得しようとしています。読み込みを開始します。クリックしてプロフィールを送信する許可を確認し、もう一度読み込みを続行しますが、最終的に空のフィールドを取得します。
何が悪いのですか?助けてください。
public class SettingsApps extends Activity {
public final String API_KEY = "492429660800628";
Facebook facebook = new Facebook(API_KEY);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
mPrefs=getPreferences(MODE_PRIVATE);
String acces_token=mPrefs.getString("acces_token",null);
long expires=mPrefs.getLong("acces_expires", 0);
if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
Session.openActiveSession(this, true, new StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
System.out.println("State= " + state);
if (session.isOpened()) {
System.out.println("Token=" + session.getAccessToken());
Request request = Request.newMeRequest(session, new GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView idText = (TextView)findViewById(R.id.id);
TextView first_nameText = (TextView)findViewById(R.id.first_name);
TextView last_nameText = (TextView)findViewById(R.id.last_name);
idText.setText(user.getId());
first_nameText.setText(user.getFirstName());
last_nameText.setText(user.getLastName());
}
if (response != null) {
System.out.println("Response=" + response);
Toast.makeText(SettingsApps.this, response.toString(),
Toast.LENGTH_LONG).show();
}
}
});
Request.executeBatchAsync(request);
}
if (exception != null) {
System.out.println("Some thing bad happened!");
exception.printStackTrace();
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
LogCat
669: INFO/ActivityManager(72): Starting: Intent { cmp=com.fitness_sport_betta/.SettingsApps } from pid 847
899: INFO/ActivityManager(72): Starting: Intent { act=SSO_WITH_FALLBACK cmp=com.fitness_sport_betta/com.facebook.LoginActivity (has extras) } from pid 847
949: INFO/System.out(847): State= OPENING
539: INFO/ActivityManager(72): Displayed com.fitness_sport_betta/com.facebook.LoginActivity: +621ms (total +851ms)
669: DEBUG/dalvikvm(389): GC_CONCURRENT freed 1685K, 41% free 6996K/11783K, external 716K/1038K, paused 16ms+11ms
420: WARN/fb4a:fb:OrcaServiceQueue(389): Exception during service
420: WARN/fb4a:fb:OrcaServiceQueue(389): com.facebook.orca.protocol.base.ApiException: remote_app_id does not match stored id
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.ApiResponseChecker.b(ApiResponseChecker.Java:74)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.ApiResponseChecker.a(ApiResponseChecker.Java:103)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.ApiResponse.g(ApiResponse.Java:184)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.Java:267)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.Java:28)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.SingleMethodRunner.a(SingleMethodRunner.Java:125)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.handler.PlatformOperationHandler.c(PlatformOperationHandler.Java:274)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.handler.PlatformOperationHandler.a(PlatformOperationHandler.Java:175)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.Java:214)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.Java:37)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.server.OrcaServiceQueue$3.run(OrcaServiceQueue.Java:168)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.Handler.handleCallback(Handler.Java:587)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.Handler.dispatchMessage(Handler.Java:92)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.Looper.loop(Looper.Java:123)
420: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.HandlerThread.run(HandlerThread.Java:60)
459: INFO/ActivityManager(72): Starting: Intent { act=com.facebook.platform.PLATFORM_ACTIVITY cat=[Android.intent.category.DEFAULT] cmp=com.facebook.katana/.platform.PlatformActivity (has extras) } from pid 847
639: INFO/ActivityManager(72): Starting: Intent { cmp=com.facebook.katana/.platform.PlatformWrapperActivity (has extras) } from pid 389
670: INFO/ActivityManager(72): Displayed com.facebook.katana/.platform.PlatformActivity: +1s87ms
779: INFO/ActivityManager(72): Displayed com.facebook.katana/.platform.PlatformWrapperActivity: +1s106ms
449: WARN/fb4a:fb:OrcaServiceQueue(389): Exception during service
449: WARN/fb4a:fb:OrcaServiceQueue(389): com.facebook.orca.protocol.base.ApiException: remote_app_id does not match stored id
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.ApiResponseChecker.b(ApiResponseChecker.Java:74)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.ApiResponseChecker.a(ApiResponseChecker.Java:103)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.ApiResponse.g(ApiResponse.Java:184)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.Java:267)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.Java:28)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.protocol.base.SingleMethodRunner.a(SingleMethodRunner.Java:125)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.handler.PlatformOperationHandler.c(PlatformOperationHandler.Java:274)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.katana.server.handler.PlatformOperationHandler.a(PlatformOperationHandler.Java:175)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.Java:214)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.Java:37)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at com.facebook.orca.server.OrcaServiceQueue$3.run(OrcaServiceQueue.Java:168)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.Handler.handleCallback(Handler.Java:587)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.Handler.dispatchMessage(Handler.Java:92)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.Looper.loop(Looper.Java:123)
449: WARN/fb4a:fb:OrcaServiceQueue(389): at Android.os.HandlerThread.run(HandlerThread.Java:60)
469: WARN/fb4a:fb:GDPDialog(389): Failed to send
469: WARN/fb4a:fb:GDPDialog(389): com.facebook.orca.ops.ServiceException: API_ERROR: API_ERROR
469: WARN/fb4a:fb:GDPDialog(389): at com.facebook.orca.ops.OrcaServiceOperation.c(OrcaServiceOperation.Java:597)
469: WARN/fb4a:fb:GDPDialog(389): at com.facebook.orca.ops.OrcaServiceOperation.c(OrcaServiceOperation.Java:38)
469: WARN/fb4a:fb:GDPDialog(389): at com.facebook.orca.ops.OrcaServiceOperation$2.run(OrcaServiceOperation.Java:562)
469: WARN/fb4a:fb:GDPDialog(389): at Android.os.Handler.handleCallback(Handler.Java:587)
469: WARN/fb4a:fb:GDPDialog(389): at Android.os.Handler.dispatchMessage(Handler.Java:92)
469: WARN/fb4a:fb:GDPDialog(389): at Android.os.Looper.loop(Looper.Java:123)
469: WARN/fb4a:fb:GDPDialog(389): at Android.app.ActivityThread.main(ActivityThread.Java:3683)
469: WARN/fb4a:fb:GDPDialog(389): at Java.lang.reflect.Method.invokeNative(Native Method)
469: WARN/fb4a:fb:GDPDialog(389): at Java.lang.reflect.Method.invoke(Method.Java:507)
469: WARN/fb4a:fb:GDPDialog(389): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:839)
469: WARN/fb4a:fb:GDPDialog(389): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:597)
469: WARN/fb4a:fb:GDPDialog(389): at dalvik.system.NativeStart.main(Native Method)
009: WARN/InputManagerService(72): Starting input on non-focused client com.Android.internal.view.IInputMethodClient$Stub$Proxy@40677138 (uid=10030 pid=847)
009: WARN/InputManagerService(72): Client not active, ignoring focus gain of: com.Android.internal.view.IInputMethodClient$Stub$Proxy@40556e88
529: INFO/ActivityManager(72): Displayed com.fitness_sport_betta/.SettingsApps: +10s839ms
最終変更:
public class SettingsApps extends Activity {
public final String API_KEY = "492429660800628";
Facebook facebook = new Facebook(API_KEY);
public final String[] permissions = {"publish_stream"};
String mUserId;
String mUserToken;
String mUserName;
String mUserEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
Session.openActiveSession(this, true, new StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
System.out.println("State= " + state);
authorizeAndPostMassage();
if (exception != null) {
System.out.println("Some thing bad happened!");
exception.printStackTrace();
}
}
});
}
}
public void authorizeAndPostMassage() {
facebook.authorize(this, permissions, new DialogListener () {
public void onComplete(Bundle values) {
Toast.makeText(SettingsApps.this, "Authorization successful", Toast.LENGTH_SHORT).show();
getProfileInformation();
}
public void onFacebookError(FacebookError e) {
Toast.makeText(SettingsApps.this, "Facebook error, try again later", Toast.LENGTH_SHORT).show();
}
public void onError(DialogError e) {
Toast.makeText(SettingsApps.this, "Error, try again later", Toast.LENGTH_SHORT).show();
}
public void onCancel() {
//Этот коллбэк никогда не срабатывает, вероятно, ошибка в SDK
Toast.makeText(SettingsApps.this, "Authorization canceled", Toast.LENGTH_SHORT).show();
}});
}
public void getProfileInformation() {
try {
JSONObject profile = Util.parseJson(facebook.request("me"));
Log.e("Profile", "" + profile);
mUserId = profile.getString("id");
mUserToken = facebook.getAccessToken();
mUserName = profile.getString("name");
mUserEmail = profile.getString("email");
runOnUiThread(new Runnable() {
public void run() {
Log.v("FaceBook_Profile",""+mUserId+"\n"+mUserToken+"\n"+mUserName+"\n"+mUserEmail);
Toast.makeText(getApplicationContext(),
"Name: " + mUserName + "\nEmail: " + mUserEmail,
Toast.LENGTH_LONG).show();
}
});
} catch (FacebookError e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
このfacebook SDK 3.0のコードは動作します!
public class FacebookInfo extends Activity {
...
String get_id, get_name, get_gender, get_email, get_birthday, get_locale, get_location;
private Session.StatusCallback fbStatusCallback = new Session.StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
if (response != null) {
// do something with <response> now
try{
get_id = user.getId();
get_name = user.getName();
get_gender = (String) user.getProperty("gender");
get_email = (String) user.getProperty("email");
get_birthday = user.getBirthday();
get_locale = (String) user.getProperty("locale");
get_location = user.getLocation().toString();
Log.d(LOG_TAG, user.getId() + "; " +
user.getName() + "; " +
(String) user.getProperty("gender") + "; " +
(String) user.getProperty("email") + "; " +
user.getBirthday()+ "; " +
(String) user.getProperty("locale") + "; " +
user.getLocation());
} catch(Exception e) {
e.printStackTrace();
Log.d(LOG_TAG, "Exception e");
}
}
}
});
}
}
};
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fbinfo);
try {
openActiveSession(this, true, fbStatusCallback, Arrays.asList(
new String[] { "email", "user_location", "user_birthday",
"user_likes", "publish_actions" }), savedInstanceState);
}
catch (Exception e) {
e.printStackTrace();
}
private Session openActiveSession(Activity activity, boolean allowLoginUI,
StatusCallback callback, List<String> permissions, Bundle savedInstanceState) {
OpenRequest openRequest = new OpenRequest(activity).
setPermissions(permissions).setLoginBehavior(SessionLoginBehavior.
SSO_WITH_FALLBACK).setCallback(callback).
setDefaultAudience(SessionDefaultAudience.FRIENDS);
Session session = Session.getActiveSession();
Log.d(LOG_TAG, "" + session);
if (session == null) {
Log.d(LOG_TAG, "" + savedInstanceState);
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, fbStatusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED) || allowLoginUI) {
session.openForRead(openRequest);
return session;
}
}
return null;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
Request.executeMeRequestAsyncは廃止されました。したがって、次のようにします。
セッションの準備ができたら:
private void getUserData(Session session, SessionState state)
{
if (state.isOpened())
{
Request.newMeRequest(session, new Request.GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
if (response != null)
{
try
{
String name = user.getName();
// If you asked for email permission
String email = (String) user.getProperty("email");
Log.e(LOG_TAG, "Name: " + name + " Email: " + email);
}
catch (Exception e)
{
e.printStackTrace();
Log.d(LOG_TAG, "Exception e");
}
}
}
}).executeAsync();
}
}
最初にオブジェクトを作成し、Facebookで検証可能にします。
private static String FACEBOOK_APP_ID = "492429660800628";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
onCreateメソッドの後:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
facebook = new Facebook(FACEBOOK_APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
loginFacebook();//this method when called when you required..
}
private void loginFacebook() {
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "email", "publish_stream",
"read_stream" }, new LoginDialogListener());
} else {
getProfileInformation();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
try {
getProfileInformation();
} catch (Exception error) {
Toast.makeText(LoginActivity.this, error.toString(),
Toast.LENGTH_SHORT).show();
}
}
public void onFacebookError(FacebookError error) {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
public void onError(DialogError error) {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
}
facebookにログインした後、この方法を試してください。
public void getProfileInformation() {
try {
JSONObject profile = Util.parseJson(facebook.request("me"));
Log.e("Profile", "" + profile);
mUserId = profile.getString("id");
mUserToken = facebook.getAccessToken();
mUserName = profile.getString("name");
mUserEmail = profile.getString("email");
runOnUiThread(new Runnable() {
public void run() {
Log.e("FaceBook_Profile",""+mUserId+"\n"+mUserToken+"\n"+mUserName+"\n"+mUserEmail);
Toast.makeText(getApplicationContext(),
"Name: " + mUserName + "\nEmail: " + mUserEmail,
Toast.LENGTH_LONG).show();
}
});
} catch (FacebookError e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
最初:ログイン
LoginManager loginManager = LoginManager.getInstance();
loginManager.registerCallback(callbackManager, new FacebookLoginCallback());
次に:プロファイル情報を取得します
// call this when login success
Profile profile = Profile.getCurrentProfile();
// getAvatar
String avatar = ImageRequest.getProfilePictureUri(profile.getId(), width, height).toString();
//register callback object for facebook result
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//By Profile Class
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
facebook_id=profile.getId();
f_name=profile.getFirstName();
m_name=profile.getMiddleName();
l_name=profile.getLastName();
full_name=profile.getName();
profile_image=profile.getProfilePictureUri(400, 400).toString();
}
//Toast.makeText(FacebookLogin.this,"Wait...",Toast.LENGTH_SHORT).show();
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
email_id=object.getString("email");
gender=object.getString("gender");
String profile_name=object.getString("name");
long fb_id=object.getLong("id"); //use this for logout
} catch (JSONException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
});
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(FacebookLogin.this,getResources().getString(R.string.login_canceled_facebooklogin),Toast.LENGTH_SHORT).show();
progress.dismiss();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(FacebookLogin.this,getResources().getString(R.string.login_failed_facebooklogin),Toast.LENGTH_SHORT).show();
progress.dismiss();
}
});
まだ新しいグラフAPI 3.0で苦労している人のために、ここではソリューションがグラフAPI 2.12以下で動作するようにしています。
//Request a read permission of user's info from Facebook
//Data provided by Facebook will be used for Firebase FireStore
LoginManager.getInstance().logInWithReadPermissions(LogIn.this, Arrays.asList("email", "public_profile"));
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
mStateOfSuccess = false;
//Dismiss any snackbar first before showing a new one
mSnackBar.dismiss();
mSnackBar.show();
Log.d(TAG, "facebook:onSuccess:" + loginResult);
//Bundle is use for passing data as K/V pair like a Map
Bundle bundle=new Bundle();
//Fields is the key of bundle with values that matched the proper Permissions Reference provided by Facebook
bundle.putString("fields","id, email, first_name, last_name, gender,age_range");
//Graph API to access the data of user's facebook account
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("Login Success", response.toString());
//For safety measure enclose the request with try and catch
try {
//The get() or getString() key should be included in Bundle otherwise it won't work properly
//If not then error dialog will be called
//First re-initialize jSON object to a new Contructor with parameter that is equal to a jSON format age range
JSONObject ageRange = new JSONObject(object.getString("age_range"));
//Log in using Facebook with Firebase
loginToFirebaseUsingFacebook(loginResult.getAccessToken()
,object.getString("first_name")
,object.getString("last_name")
//Then get again get a string from object itself for the minimum age range
//The idea is that we need to get minimum age only written in string format
//not the whole age range data that is written in jSON format
,ageRange.getString("min")
,object.getString("gender")
,object.getString("email")
);
}
//If no data has been retrieve throw some error
catch (JSONException e) {
ErrorDialog(e.getMessage(),"facebookAuth");
}
}
});
//Set the bundle's data as Graph's object data
request.setParameters(bundle);
//Execute this Graph request asynchronously
request.executeAsync();
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
ErrorDialog("Request has canceled.","facebookAuth");
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
ErrorDialog(String.valueOf(error),"facebookAuth");
}
});
}
プロダクションでを使用する場合、年齢範囲を使用するにはApp Reviewが必要です。つまり、自分のアカウント(管理者または開発者アカウント)でこれを使用しようとするだけです。訪問:https://developers.facebook.com/docs/facebook-login/permissions/?locale=en_US#reference-public_profile 詳細については。