I am trying to use Google Drive in my Android Application - basically to read PDF files from Google Drive - but now just trying to connect and fetch any file from Drive.
So I did the following:
-
Created a new Google Cloud project, enabled Drive API and generated an API key and OAuth client ID (both having the same SHA1 - is that correct?)

-
Used the following code on my MainActivity.kt:
class MainActivity : AppCompatActivity() { private val fields = "nextPageToken, files(id, name)" private lateinit var readButton: Button private var googleSignInClient: GoogleSignInClient? = null private var isFileRead = false private val accessDriveScope: Scope = Scope(Scopes.DRIVE_FILE) private val scopeEmail: Scope = Scope(Scopes.EMAIL) private var launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK){ val data: Intent? = result.data try{ val task = GoogleSignIn.getSignedInAccountFromIntent(data) task.getResult(ApiException::class.java) checkForGooglePermissions() } catch (e: ApiException){ } } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) readButton = findViewById(R.id.read) readButton.setOnClickListener{ isFileRead = true val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .requestScopes(Scope(DRIVE_FILE)) .build() googleSignInClient = GoogleSignIn.getClient(this,gso) val signInIntent = googleSignInClient?.signInIntent launcher.launch(signInIntent) } } private fun checkForGooglePermissions() { if (!GoogleSignIn.hasPermissions( GoogleSignIn.getLastSignedInAccount(this), accessDriveScope, scopeEmail )){ GoogleSignIn.requestPermissions( this, 1, GoogleSignIn.getLastSignedInAccount(this), accessDriveScope, scopeEmail ) } else { lifecycle.coroutineScope.launch { driveSetUp() } } } private suspend fun driveSetUp(){ val mAccount = GoogleSignIn.getLastSignedInAccount(this) val credential = GoogleAccountCredential.usingOAuth2( this, Collections.singleton(Scopes.DRIVE_FILE) ) credential.selectedAccount = mAccount?.account val googleDriveService = Drive.Builder( NetHttpTransport(), GsonFactory(), credential ).setApplicationName("My Drive Test").build() if (isFileRead){ //Read from drive withContext(Dispatchers.Default){ try { val fileListJob = async {listDriveFiles(googleDriveService)} val fileList = fileListJob.await() for (item in fileList){ Log.d("MyLog", item.name) } } catch (e: Exception){ //Handle error } } } } private fun listDriveFiles(mDriveService: Drive): List { var result: FileList var pageToken: String? = null do { result = mDriveService.files().list() .setQ("mimeType=text/plain") .setSpaces(DRIVE) .setFields(fields) .setPageToken(pageToken) .execute() pageToken = result.nextPageToken } while (pageToken != null) return result.files } } - Once running the application it prompts me to connect to my Google account but then it fails in "val fileListJob = async {listDriveFiles(googleDriveService)}" with the following response:

[Edit 1:]
The only link between my Android app and the Drive API is that I added my android package name to both API Key and OAuth Client ID:


What am I doing wrong? I do have an API key..
[Edit 2]:
I followed up on this and also had to use getAccountsByType() as indicated here but unfortunately while using:
val am: AccountManager = AccountManager.get(this) // "this" references the current Context val accounts: Array = am.getAccountsByType("com.google") I get an empty list (even though I have 3 accounts defined in my mobile phone). So instead I tried using this:
val accounts: Array = am.accounts and it returned back 1 account (my main gmail address) and it shows its type as
com.osp.app.signin
What should I do next in order to login to my Drive account? I didn't find any complete instruction on how to do so - would really appreciate your assistance.
P.S.: I have the following permissions in my manifest:
Источник: https://stackoverflow.com/questions/780 ... -api-fails
Мобильная версия