Google Drive 403未认证日限额超限报错(debug正常release失效)解决方案
问题描述
我开发了一款应用,会将app-database.db文件存储到用户Google Drive的appDataFolder中,目前应用尚未发布,仅我本人作为测试用户使用。
- 我已在
Google Cloud Console中生成了debug和release两种构建类型对应的API key和OAuth Client ID,项目已接入Firebase Console,且google-services.json文件已放置在应用主模块目录下。 OAuth Consent配置已完成,状态设为In Production,申请的授权域为.../auth/drive.appdata和.../auth/drive.file,二者均为non-sensitive scopes,无需平台验证。同时Google Drive API已在Google Cloud Console中启用。
核心代码
跳转用户登录页代码:
val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Scope(DriveScopes.DRIVE_FILE)) .requestScopes(Scope(DriveScopes.DRIVE_APPDATA)) .requestEmail() .build() startActivityForResult(GoogleSignIn.getClient(this, signInOptions).signInIntent, SIGN_IN_REQ_CODE)
检查登录结果代码:
GoogleSignIn.getSignedInAccountFromIntent(result) .addOnSuccessListener { account: GoogleSignInAccount -> if (GoogleSignIn.hasPermissions(account, *scopes)) { val drive = GoogleAccountCredential.usingOAuth2(this@SomeActivity, credentialScopes) .apply { selectedAccount = account.account } .let { Drive.Builder(AndroidHttp.newCompatibleTransport(), GsonFactory(), it) .setApplicationName("APP_NAME_FROM_CONTEST_SCREEN") .build() } // ... } else { GoogleSignIn.requestPermissions(this@SomeActivity, PERMISSIONS_REQ_CODE, account, *scopes) } }.addOnFailureListener { // ... } private val scopes = arrayOf(Scope(DriveScopes.DRIVE_FILE), Scope(DriveScopes.DRIVE_APPDATA), Scope(Scopes.PROFILE), Scope(Scopes.OPEN_ID), Scope(Scopes.EMAIL)) private val credentialScopes = listOf(DriveScopes.DRIVE_FILE, DriveScopes.DRIVE_APPDATA)
问题现象
使用Drive API Client v3上传或下载任意文件夹下的文件时,debug构建包运行完全正常,但release构建包会返回如下报错:
403 Forbidden { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", "extendedHelp": "https://code.google.com/apis/console" } ], "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." }
问题原因与解决方案
release构建配置中开启了minifyEnabled true,问题根源是缺少Google相关API库的Proguard混淆规则,添加如下规则后问题解决:
# 保留反射调用需要的泛型类型和@Key注解 -keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault -keepclassmembers class * { @com.google.api.client.util.Key <fields>; } # 避免旧平台版本编译时google-http-client-android相关警告 -dontwarn com.google.api.client.extensions.android.** # 避免旧平台版本编译时google-api-client-android相关警告 -dontwarn com.google.api.client.googleapis.extensions.android.** # 避免旧平台版本编译时google-play-services相关警告 -dontwarn com.google.android.gms.**
内容的提问来源于stack exchange,提问作者Reza
相关产品推荐
相关产品推荐

