Search code examples
androidgoogle-drive-android-api

Google Drive Api authentication in Android app


I am using google drive in my Android app my application takes photos and uploads to drive automatically, but problem is for authentication it asks to choose google account for the first time when i open the app ,can i programatically give my credentials and make authentication done so that i can avoid this step of choosing and authentication with UI.


Solution

  • You may refer with this link. This can be done with the Oauth2 Playground at https://developers.google.com/oauthplayground.

    Steps:-

    1. Create the Google Account (eg. my.drive.app@gmail.com)
    2. Use the API console to register the mydriveapp (https://console.developers.google.com/apis/credentials/oauthclient?project=mydriveapp or just https://console.developers.google.com/apis/)
    3. Create a new set of credentials (NB OAuth Client ID not Service Account Key and then choose "Web Application" from the selection)
    4. Include https://developers.google.com/oauthplayground as a valid redirect URI
    5. Note the client ID (web app) and Client Secret
    6. Login as my.drive.app@gmail.com
    7. Go to Oauth2 playground
    8. In Settings (gear icon), set * Oauth flow: server * Access type: offline * Use your own OAuth credentials: TICK * Client Id and Client Secret: from step 5
    9. Click Step 1 and choose Drive API https://www.googleapis.com/auth/drive (having said that, this technique also works for any of the Google APIs listed)
    10. Click Authorize APIs. You will be prompted to choose your Google account and confirm access
    11. Click Step 2 and "Exchange Authorization code for tokens"
    12. Copy the returned Refresh Token and paste it into your app, source code or in to some form of storage from where your app can retrieve it.

    You may also use Service Account. However, it would create a pseudo Drive account for the app, rather than allow the app to access your regular Drive account. Check this.