使用 GoogleCredentials 在 Google 云端硬盘上创建文件夹

问题描述 投票:0回答:1

我有以下功能来授权访问 Google Drive,以便使用 GoogleCredentials 在 Google Drive 上创建文件夹。

implementation("com.google.android.gms:play-services-auth:21.2.0")
implementation("com.google.api-client:google-api-client:2.5.0")
implementation("com.google.apis:google-api-services-drive:v3-rev20240521-2.0.0")

implementation("com.google.http-client:google-http-client-android:1.26.0")
// Google Sign in
fun signIn(context: Context) {

    val authorizationRequest = AuthorizationRequest.builder().setRequestedScopes(SCOPES).build()

    Identity.getAuthorizationClient(context as Activity).authorize(authorizationRequest)
        .addOnSuccessListener { authorizationResult ->

            if (authorizationResult.hasResolution()) {

                // Access needs to be granted by the user
                val pendingIntent = authorizationResult.pendingIntent
                try {
                    startIntentSenderForResult(
                        context, pendingIntent!!.intentSender, 999, null, 0, 0, 0, null
                    )
                } catch (e: IntentSender.SendIntentException) {
                    Log.i("@@@", "Authorization failed: " + e.message)
                }

            } else {

                // Access authorized, do stuff
                Log.i("@@@", "Access granted")

                val token = authorizationResult.accessToken
                val credentials = GoogleCredentials.create(AccessToken(token, null))
                val requestInitializer = HttpCredentialsAdapter(credentials)

                val googleDriveService = Drive.Builder(
                    AndroidHttp.newCompatibleTransport(), //NetHttpTransport(),
                    GsonFactory(), requestInitializer
                )
                    .setApplicationName(context.getString(R.string.app_name))
                    .build()

                Log.i("@@@", googleDriveService.toString())

                try {
                    // Create folder on Google Drive
                    // Folder metadata
                    val folderMetadata =
                        File().setMimeType("application/vnd.google-apps.folder")
                            .setParents(listOf(ROOT))
                            .setName(FOLDER_NAME).setStarred(true)
                            .setFolderColorRgb("#FF0000")       // Red color

                    // Create folder
                    googleDriveService.files().create(folderMetadata).execute()     // This crashes the app, error msg: null

                } catch (e: Exception) {
                    Log.e("@@@", e.message.toString())
                }
            }
        }.addOnFailureListener { e -> Log.e("@@@", "Failed to authorize", e) }
}

就语法而言,一切似乎都很好,但是未创建文件夹,并且在测试以下内容后: googleDriveService.files().create(folderMetadata).execute() 导致崩溃,错误消息为“null”

我从按钮的 onClick = { signIn(context) } 调用上述函数

我真的很感激任何帮助,我已经为此苦苦挣扎了好几个星期。谢谢。

kotlin google-drive-api android-jetpack-compose
1个回答
0
投票

我在服务器端使用 Google Drive API(JVM 上的 Kotlin,而不是 Android 上的 Kotlin),所以我无法对此进行测试,但请尝试更改:

val credentials = GoogleCredentials.create(AccessToken(token, null))

import com.google.api.services.drive.DriveScopes
val DRIVE_SCOPES: List<String> = arrayOf(DriveScopes.DRIVE).toList()

val credentials = GoogleCredentials.create(AccessToken(token, null))
                     .createScoped(DRIVE_SCOPES)
© www.soinside.com 2019 - 2024. All rights reserved.