GoogleAccountCredential reuse auth - Google Drive - android

I'm using this method to auth to Google Drive:
https://developers.google.com/drive/quickstart-android
Choosing account is working great. Now I want to store user credentials in prefs. I can save account name and then resore it. I want to reuse credentials for future use.
Is it possible to reauth using Google Play Services and GoogleAccountCredential?
This is my scenario:
StartActivity
Search for savedCredentials
If NOT -> show ACCOUNT PICKER (this is working)
If I choose account - saveCredentians to preferences.
Now I close my app and run again.
StartActivity
Search for savedCredentials
YES - there are saved credentials. I want to use them.
I'm trying something like this:
credential = GoogleAccountCredential.usingOAuth2(GoogleDriveBackup.this, DriveScopes.DRIVE_FILE);
//use saved account (stored[0] is OK)
credential.setSelectedAccountName(stored[0]);
credential.getToken();
service = getDriveService(credential);
Where:
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(),
new GsonFactory(), credential).build();
}
This isn't working. I'm getting 500 Internal Server errors from GoogleJsonResponseExcelption. What's the best way to do that using Google Play Services for Android.

If what you are trying to do is avoiding that the user has to re-authorize your app then you don't need to save the credential Object. All yo need to do is save the Account the user chose using the account picker.
When re-creating a credential object the next time your app is used, the play services will know that the user has already granted you access before and you won't have to show the auth Intent (available in the Exception thrown).
You do need to provide a way for the user to change the account he chose though.

Related

Changing email in google workspace effect on Firebase auth identity

I had a user of my firebase android app change their email address in google workspace according to these steps to change user email.
My app uses (only) google to sign in:
val cred = GoogleAuthProvider.getCredential(account.idToken, null)
FirebaseAuth.getInstance().signInWithCredential(cred)
After the change the user is able to sign in with their new email and my app still identifies them with the same UID, but when they sign in they still see their old profile pic and their old email which the app retrieves:
FirebaseAuth.getInstance().currentUser?.let { user ->
user.email
user.displayName
user.photoUrl
}
All of those properties still refer to the old email, old profile pic and old display name, but we are mainly concerned about the email property.
I'd like to know why we're still seeing the old email, and is this something that needs to be fixed on the application side or the admin/google workspace side?
Firebase Authentication creates a cached copy of the pertinent information from the OAuth provider when it first creates the account. This information is not updated afterwards, unless you do so yourself with the Admin SDK.

Authentication with Dropbox Api V2 [duplicate]

I've just started out and I'm following this tutorial
https://www.dropbox.com/developers/documentation/java#tutorial
But there's no login. Nothing asks you for a username, password. That means I can't actually get a GUI that every app with "Share to Dropbox" option has.
Am I stuck with one account? Do I have to find a way to get a person's ACCESS_TOKEN or is there a more elegant GUI solution out there(like with Google Drive and their intentsenders)?
To use the Dropbox API v2 in Android, you should use the API v2 Java SDK. There's an example Android app that uses it included with the SDK. You should refer to that as an example of how to implement the app authorization flow, which is accomplished via OAuth 2. That requires the user to authorize your app with Dropbox, by signing in to Dropbox if necessary. After that, your app can store and re-use the resulting access token for that user, as the example does here.
Implementing it that way allows any user to connect their Dropbox account to your app. You can also handle multiple accounts per instance of your app if you want.
Unfortunately, there isn't much out there in terms of documentation for this flow. Here is how I was able to successfully authenticate users. First, you want to launch the authentication flow with Dropbox's auth activity:
import com.dropbox.core.android.Auth
....
Auth.startOAuth2Authentication(context, context.getString(R.string.dbx_api_app_key))
After a user has successfully authenticated, call the following method in the onResume method of the activity you started the Dropbox activity from:
#Override
public void onResume() {
super.onResume();
String token = Auth.getOAuth2Token()
}
The token that you receive here should be used when you create your instance of the DbxClientV2 like so:
DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder(MyUtils.getVersionText(context))
.withHttpRequestor(OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient()))
.build()
DbxClientV2 dbxClient = DbxClientV2(requestConfig, accessToken)
You'll also need the following dependencies in your build.gradle file:
implementation 'com.squareup.okhttp:okhttp:2.7.5'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'

Smart Lock SDK is not retrieving credential sets correctly for NexusPlayer( Android TV )

Im currently setting up smart lock for our tv app in nexus player.
The problem is that even though i already saved few credential sets for my google account through our mobile app( which implemented the smart lock sdk long ago ), i cant retrieve all of them in our tv app.
in our Tv app , i can only retrieve one of those credentials.
And whats even stranger is that if i try to save my one of my credentials through my tv app, which is already existing in the cloud ( the account begins with 6249) , it will be saved and you can see duplicated credentials . And after that i can retrieve this credentials from smart lock in the tv app.
Noted that the app's domain is the same for both tv app and our mobile app.
You can retrieve saved credentials through CredentialsApi.request() method.
Auth.CredentialsApi.request(mCredentialsClient, mCredentialRequest).setResultCallback(
new ResultCallback<CredentialRequestResult>() {
#Override
public void onResult(CredentialRequestResult credentialRequestResult) {
if (credentialRequestResult.getStatus().isSuccess()) {
// See "Handle successful credential requests"
onCredentialRetrieved(credentialRequestResult.getCredential());
} else {
// See "Handle unsuccessful and incomplete credential requests"
resolveResult(credentialRequestResult.getStatus());
}
}
});
Handle multiple saved credentials:
When user input is required to select a credential, the getStatusCode() method returns RESOLUTION_REQUIRED. In this case, call the status object's startResolutionForResult() method to prompt the user to choose an account. Then, retrieve the user's chosen credentials from the activity's onActivityResult() method by passing Credential.EXTRA_KEY to the getParcelableExtra() method.
Here's a documentation how to Store credentials: https://developers.google.com/identity/smartlock-passwords/android/store-credentials#store_credentials

Can't set Google Account Credential in Android

I'm trying to use the Google Drive REST API - using the exact sample code found at Android REST Quickstart. However I cannot set an account name for the Google Account Credential object in the the onActivityResult method.
I don't get it as I've triple checked that mCredential.setSelectedAccountName(accountName); is definitely being passed an email string for the account name, but when I try and use mCredential.getSelectedAccountName() it always returns null immediately after it has supposedly been set. This means that the account picker is constantly being called but never sets an account name.
Someone please help as this is driving me crazy!
If You are running your application on android version 6.+ then make sure that your app has all the required permission allowed. You can check this by going Setting->apps->your_app->apps permission.

Google Play Services - force to show permission window (grant access)

I'm using Google Play Services in my apps but lately I found something strange and I don't know how to deal with it.
If you are using Google Play Services you know that for the fist time, before you'll generate token you need to give your app permission (grant access) to use selected API.
After that, your app will be visible here: https://accounts.google.com/IssuedAuthSubTokens
And my app is there. Everything was working. I wanted to test it from the scratch, I revoke access to my app and ... it stops working. I can't force Google Play Services to show this window after revoking access to my app. I don't have permission to Google API but Google should show me permission window to re-add it.
Is there a way to show this window again?
I'm using GoogleAccountCredential.usingOAuth2 function to get user token and data (with all exceptions like UserRecoverableAuthIOException, startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION) ...).
As I understand, GoogleAccountCredential uses the Google Play Services to manage the OAuth2 flow and all you need to provide is the username. After revoking access it's not working.
Using Google Play Services:
http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html
Add https://www.googleapis.com/auth/userinfo.profile to your scope.
Example:
String scope="oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
final String token = GoogleAuthUtil.getToken(context, "xxxx#gmail.com", scope);
OR "brute force"
Intent res = new Intent();
res.addCategory("account:xxxx#gmail.com");
res.addCategory("scope:oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile");
res.putExtra("service", "oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile");
Bundle extra= new Bundle();
extra.putString("androidPackageName","com.your.package");
res.putExtra("callerExtras",extra);
res.putExtra("androidPackageName","com.your.package");
res.putExtra("authAccount","xxxx#gmail.com");
String mPackage = "com.google.android.gms";
String mClass = "com.google.android.gms.auth.TokenActivity";
res.setComponent(new ComponentName(mPackage,mClass));
startActivityForResult(res,100);
Now, when you revoke the access here https://accounts.google.com/IssuedAuthSubTokens the application shows you the window for permission again in the device.

Categories

Resources