Mic*_*ons 3 java android login oauth-2.0 firebase
我一直在尝试为我的应用设置谷歌登录.
我按照这里的教程:
https://developers.google.com/identity/sign-in/android/start-integrating
我做了一切.我可以登录登录并退出.
然后我添加了一个异步任务来获取一个令牌,它似乎成功检索.它实现如下:
private class GetIdTokenTask extends AsyncTask<Void, Void, String> {
private static final String SERVER_CLIEdNT_ID = "749433126040-ca4gfj7ucuh0m2suo3230u03o3d7doni.apps.googleusercontent.com";
@Override
protected String doInBackground(Void... params) {
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
Account account = new Account(accountName, GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
String scopes = "audience:server:client_id:" + SERVER_CLIEdNT_ID; // Not the app's client ID.
try {
return GoogleAuthUtil.getToken(getApplicationContext(), account, scopes);
} catch (IOException e) {
Log.e(TAG, "Error retrieving ID token.", e);
return null;
} catch (GoogleAuthException e) {
Log.e(TAG, "Error retrieving ID token. Google exception", e);
return null;
}
}
@Override
protected void onPostExecute(String result) {
Log.i(TAG, "ID token: " + result);
if (result != null) {
idToken = result;
Toast.makeText(context, "token is " + result, Toast.LENGTH_LONG).show();
} else {
// There was some error getting the ID Token
// ...
Toast.makeText(context, "token is null", Toast.LENGTH_LONG).show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在运行该方法并获取令牌后,我运行通用firebase代码以连接到Firebase(已经设置了谷歌应用程序,将客户端ID放入firebase,启用它等)我从https获取代码:/ /www.firebase.com/docs/android/guide/login/google.html
并实现如下:
public void loginFireBase() {
Firebase ref = new Firebase("https://boiling-fire-944.firebaseio.com");
ref.authWithOAuthToken("google", idToken, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
// the Google user is now authenticated with your Firebase app
Toast.makeText(context, "user succesfully authenticated with firebase", Toast.LENGTH_LONG).show();
Toast.makeText(context, idToken, Toast.LENGTH_LONG).show();
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
///////THIS IS THE PART WHERE THE ERROR IS GENEREATED FROM//////////
Log.v("firebase problem", firebaseError.toString());
Toast.makeText(context, "I'm an authenticated error" + "id Token was " + idToken, Toast.LENGTH_LONG).show();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
所以最终,我登录,我得到一个令牌,然后将其传递给firebase authWithOAuthToken方法,然后我得到错误:
FirebaseError:提供的身份验证凭据无效.
谁能在我的代码中看到任何问题?我有一种感觉,令牌无效,但无法找到检查其有效性的方法.
告诉我,如果你需要我发布更多,我试着保持简短.
感谢任何可以提供帮助的人!
每当我需要在Android上使用Firebase进行身份验证时,我都会回到Firebase Android登录演示.以下是该应用获取Google身份验证的OAuth令牌的方式:
String scope = String.format("oauth2:%s", Scopes.PLUS_LOGIN);
token = GoogleAuthUtil.getToken(
GoogleOAuthActivity.this,
Plus.AccountApi.getAccountName(mGoogleApiClient),
scope);
Run Code Online (Sandbox Code Playgroud)
看起来您正在为用户获取跨客户端ID,这不是有效的OAuth令牌.
更新(20160308):现在我改为查看FirebaseUI库,其中包含此功能并且更新.
| 归档时间: |
|
| 查看次数: |
2896 次 |
| 最近记录: |