问题
I wanted the ability to upload and download from a users Google drive account.
Uploading and download works fine, the problem is authorizing the account.
So far I was using the android debug keystore for building the actual download and upload classes, but when I wanted to switch over to my own app it stopped working.
I have enabled the Drive API in Google drive console, and have downloaded the client_secret.json file and put it in the assets folder. I do not know how to use it in authorization.
So basically how to authorize an account to use the Google drive API in android. Please note that my DriveScope is DRIVE, meaning full control of a given Google drive account.
This is the code i am using to access google drive files. I followed the tutorial on this link: https://developers.google.com/drive/web/quickstart/android
googleAccountCredential = GoogleAccountCredential.usingOAuth2(
getActivity().getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(getArguments().getString("account"));
driveService = new Drive.Builder(
transport, jsonFactory, googleAccountCredential)
.setApplicationName("myAPP")
.build();
回答1:
It is very likely just a mismatch of you authorization items. For an Android app, the authorization consists of 2 essential items that must be in order in your developers console (OMG, they re-shuffled it again !!!). You don't need the 'client_secret.json'.
- APIs & auth -> Credentials -> Credentials -> Android client 1 'SHA1' MUST BE IN SYNC with your 'Pakage name'
- APIs & auth -> Credentials -> OAuth consent screen MUST have your 'Product Name' and 'Email address'
I'm guessing that by switching to the 'release' version of your app, the 'Package name' / 'SHA1' you are producing are out of sync with the Developers console (your IDE is pointing to a different keystore, thus pulling incorrect SHA1).
Here is the 'last ditch' check I use when I get in trouble.
- Find the APK that's not working. Open the '*.apk' file in an unzipper (7-zip) and find 'META-INF\CERT.RSA' file in the archive. Unzip it, for instance to your desktop
- Run 'keytool -printcert -file .\desktop\CERT.RSA'.
- You should see the 'SHA1' of the APK file. Compare it with the 'SHA1' in the dev console. Check the package name as well (it is in your manifest). The 'Package Name' / 'SHA1' pair is what authorizes the app version in Google Drive.
Good Luck
来源:https://stackoverflow.com/questions/32834901/android-authorize-google-drive-api