Accesing google drive API for a specific account

家住魔仙堡 提交于 2019-12-20 05:53:47

问题


I'm trying to access google drive through my android app for a specific account. In addition, I also have sign in with google option, so that users can login to the app through google+ and this will be the google account which play services is using.

However, I want to store some files on a specific google drive account say abcd@gmail.com. I tried using the GoogleAPIClient by specifying DRIVE API, required scopes and an account name as 'abcd@gmail.com', which looks something like below:

serviceClient = new GoogleApiClient.Builder(this)
         .addApi(Drive.API)
         .setAccountName("abcd@gmail.com").addConnectionCallbacks(this)
         .addScope(Drive.SCOPE_FILE)  
         .addOnConnectionFailedListener(this)
         .build();
serviceClient.connect();

But, I'm getting an error :

The specified account does not exist on this device. Please choose different account.

How can I connect to specific/static account of google to access drive APIs.

Thanks Saurabh


回答1:


It is my belief that you can only use accounts (xxx@gmail.com) that are 'registered' on your device (Settings.. Accounts). That means, you have to invoke the Android account picker activity to let the user register the account (it would be a security disaster if your app could access any account only knowing it's email - no password, right?).

If you want to dig into the code that does it, just follow the 'REQ_ACCPICK' constant in this code. It shows you the sequence you have to use. Especially:

  • Account picker is invoked if the app does not have 'default email' to work with (GDAA.init() failed).
  • Account picker is invoked by the user from the app's menu so the user can select / add a new account (onOptionsItemSelected()).

It should be noted, that a similar account picker is invoked by GDAA's 'GoogleApiClient.Builder()' (your code above) if you do not pass any email. I believe there is a method somewhere that resets your 'serviceClient', allowing the GDAA builder to re-invoke the account picker again (see clearDefaultAccountAndReconnect()).
I have never used this approach, you may investigate this path further. I think I did not follow this, since I could not retrieve the current account selected by the user. And the knowledge of who the current user is was essential for my application (caching account specific data, etc...)

Good Luck



来源:https://stackoverflow.com/questions/32840511/accesing-google-drive-api-for-a-specific-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!