我按照this tutorial制作一个小应用程序登录并说出你好+用户名。
问题是:我只能使用我的帐户登录,但无法使用其他帐户登录。 一些示例代码需要登录,例如HelloFacebookSample或Scrumptious,就会出现此问题。 Logcat没有显示任何错误。
所以请帮我用其他帐户登录。提前谢谢!
编辑(解决): 我刚刚找到原因:我的应用程序处于沙盒模式,只是禁用沙盒模式解决了问题。感谢任何人的帮助。
我的代码:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello " + user.getName() + "!");
}
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
使用我的帐户登录时的屏幕截图:显示你好+我的名字(NguyễnViệtAnh)
使用其他帐户登录时的屏幕截图:白色屏幕
答案 0 :(得分:1)
这是预期的行为。基本上,Facebook的登录是SSO(单点登录),因此强烈期望用户在其设备上只有一个帐户。
我自己试图找到一种方法来获取Facebook SDK以允许用户登录其他帐户,但它不起作用。
也许可以通过清除缓存来捏造它,但这不会帮助那些在手机上使用正版Facebook应用程序的用户。
我最终做的是进入网络工作流而不是本机应用。我可以为这项任务推荐抄写员。
https://github.com/fernandezpablo85/scribe-java
如果你选择使用Scribe,这是我登录的活动。
public class FacebookScribeLogin extends FragmentActivity{
private static final String TAG = FacebookScribeLogin.class.getSimpleName();
private final static String CALLBACK = "http://localhost:3000/";
private WebView mWebView;
private ProgressDialog mProgressDialog;
private OAuthService mAuthService;
private SyncStatusUpdaterFragment mSyncStatusUpdaterFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
new GetLoginPage().execute();
}
private class GetLoginPage extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
//set up service and get request token as seen on scribe website
//https://github.com/fernandezpablo85/scribe-java/wiki/Getting-Started
mAuthService = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey(getString(R.string.facebook_api_key))
.apiSecret(getString(R.string.facebook_api_secret))
.scope("read_stream, publish_stream, manage_notifications, publish_actions, manage_pages")
.callback(CALLBACK)
.build();
return mAuthService.getAuthorizationUrl(null);
}
@Override
protected void onPostExecute(String authURL) {
//send user to authorization page
android.webkit.CookieManager.getInstance().removeAllCookie();
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//check for our custom callback protocol otherwise use default behavior
if (url.startsWith(CALLBACK)) {
GetAccessToken getAccessToken = new GetAccessToken(url);
getAccessToken.execute();
return true;
}
if(mProgressDialog == null){
mProgressDialog = ProgressDialog.show(FacebookScribeLogin.this, null,
String.format(getString(R.string.talking_to_x), getString(R.string.facebook)), true, false);
}
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
if(mProgressDialog != null){
mProgressDialog.hide();
mProgressDialog = null;
}
}
});
mWebView.loadUrl(authURL);
}
}
private class GetAccessToken extends AsyncTask<Void, Void, Void>{
private String mUrl, mToken, mSecret;
private GetAccessToken(String url) {
mUrl = url;
}
@Override
protected Void doInBackground(Void... voids) {
Uri uri = Uri.parse(mUrl);
String verifierStr = uri.getQueryParameter("code");
Verifier verifier = new Verifier(verifierStr);
//save this token for practical use.
Token accessToken = mAuthService.getAccessToken(null, verifier);
mToken = accessToken.getToken();
mSecret = accessToken.getSecret();
return null;
}
@Override
protected void onPostExecute(Void s) {
//mToken - save your mToken somehwere and perhaps use a graph API call for user details
}
}
}
答案 1 :(得分:1)
您可以使用不同的Facebook用户ID登录: