驱动器的SDK为Android AuthIntent为null [英] Drive SDK for Android AuthIntent is null
问题描述
我想谷歌驱动器集成到我的Android应用程序,这样我可以在用户的硬盘存储文件。我已经在App控制台启用两个驱动SDK和API驱动器,并设置了我的Oauth 2的ClientID我的SHA1指纹。至于我可以告诉大家,一切看起来都对谷歌的到底好不好。
这里的code,它初始化我Drive对象。
私人布尔initializeService(字符串帐户名){
如果(服务!= NULL){
返回true;
}
GoogleAccountCredential凭证= GoogleAccountCredential.usingOAuth2(mContext,DriveScopes.DRIVE);
mAuthIntent = NULL; 如果(帐户名!= NULL){
尝试{
credential.setSelectedAccountName(帐户名);
Log.d(DriveHelper,credential.getToken());
服务= getDriveService(凭证);
}赶上(例外前){
如果(前的instanceof UserRecoverableAuthException){
UserRecoverableAuthException authException =(UserRecoverableAuthException)前;
mAuthIntent = authException.getIntent();
mError code = CloudHelper.CONNECTION_AUTHENTICATE;
((活动)mContext).startActivityForResult(mAuthIntent,REQUEST_AUTHORIZATION);
}其他{
Log.e(DriveCloudHelper,错误检索身份验证令牌,前);
} 返回false;
}
getAppFolder(); 返回mFileLocation!= NULL;
} 活动活动=(活动)mContext;
activity.startActivityForResult(credential.newChooseAccountIntent(),REQUEST_ACCOUNT_PICKER);
返回false;
}私人驱动器getDriveService(GoogleAccountCredential凭证){
返回新Drive.Builder(AndroidHttp.newCompatibleTransport(),新GsonFactory(),凭据)
.setApplicationName(驱动器助手)
。建立();
}
这些方法称为在后台线程和工作得很好,在帐户选择器出现时,授权窗口,都好。我的 getAppFolder
方法查找特定的文件夹,并创建它,如果没有present没有任何问题(在同一个后台线程)。
然而,当我去上传文件到驱动器,第一个操作我尝试一个新的后台线程导致 UserRecoverableAuthException
,但是当我打电话发生新的异常 getIntent
,因为没有意图提供,并且详细消息是 AppDownloadRequired
。
下面是一个的产生异常的方法。
私人字符串getIdFromFolder(字符串位置,字符串名称,布尔创建){
尝试{
串maskedName = name.replaceAll(/,);
查询字符串=标题包含+ maskedName +和+位置+父母;
。文件清单列表= service.files()名单()setMaxResults(1).setQ(查询).execute();
清单<文件>文件= list.getItems(); 如果(files.isEmpty()){
返回创建? createSubFolder(位置,名称):空;
} 返回files.get(0).getId();
}赶上(IOException异常五){
//在这里吃的例外
Log.w(DriveCloudHelper,E);
}
返回创建? createSubFolder(位置,名称):空;
}
和这里的堆栈跟踪:
com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
在com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:222)
在com.google.api.client.http.Htt prequest.execute(HTT prequest.java:836)
在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
在net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.getIdFromFolder(DriveCloudHelper.java:269)
在net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.uploadDatabaseToCloud(DriveCloudHelper.java:405)
在net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.synchFileToCloud(DriveCloudHelper.java:368)
在net.wishfullthinking.groceryhelper.interfaces.CloudHelper.mergeCloudStoreCollection(CloudHelper.java:472)
在net.wishfullthinking.groceryhelper.interfaces.CloudHelper.access $ 10美元(CloudHelper.java:454)
在net.wishfullthinking.groceryhelper.interfaces.CloudHelper $ 4.run(CloudHelper.java:444)
在java.lang.Thread.run(Thread.java:856)
com.google.android.gms.auth.UserRecoverableAuthException:引起AppDownloadRequired
在com.google.android.gms.auth.GoogleAuthUtil.getToken(来源不明)
在com.google.android.gms.auth.GoogleAuthUtil.getToken(来源不明)
在com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
在com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
... 11更多
尝试使用 mContext.getApplicationContext()
在 GoogleAccountCredential.usingOAuth2
通话,而不是 mContext
作为,似乎有固定的类似的问题在其他地方的。
I'm trying to integrate Google Drive into my Android app so that I can store files in a user's Drive. I've enabled both Drive SDK and Drive API in the App Console and set up my Oauth 2 ClientIDs with my SHA1 fingerprint. As far as I can tell, everything looks good on Google's end.
Here's the code that initializes my Drive object.
private boolean initializeService(String accountName) {
if(service != null) {
return true;
}
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(mContext, DriveScopes.DRIVE);
mAuthIntent = null;
if(accountName != null) {
try {
credential.setSelectedAccountName(accountName);
Log.d("DriveHelper", credential.getToken());
service = getDriveService(credential);
} catch(Exception ex) {
if(ex instanceof UserRecoverableAuthException) {
UserRecoverableAuthException authException = (UserRecoverableAuthException)ex;
mAuthIntent = authException.getIntent();
mErrorCode = CloudHelper.CONNECTION_AUTHENTICATE;
((Activity)mContext).startActivityForResult(mAuthIntent, REQUEST_AUTHORIZATION);
} else {
Log.e("DriveCloudHelper", "Error retrieving auth token", ex);
}
return false;
}
getAppFolder();
return mFileLocation != null;
}
Activity activity = (Activity)mContext;
activity.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
return false;
}
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
.setApplicationName("Drive Helper")
.build();
}
These methods are called on a background thread and work just fine, the Account Picker appears, the Authorization window appears, all good. My getAppFolder
method looks for a specific folder and creates it if not present without any issue (on the same background thread).
However, when I go to upload a file to Drive, the first operation I try on a new background thread causes a UserRecoverableAuthException
, but a new exception occurs when I call getIntent
because there's no Intent provided, and the detail message is AppDownloadRequired
.
Here's the method that's generating the exception.
private String getIdFromFolder(String location, String name, boolean create) {
try {
String maskedName = name.replaceAll("/", "");
String query = "title contains '" + maskedName + "' and '" + location + "' in parents";
FileList list = service.files().list().setMaxResults(1).setQ(query).execute();
List<File> files = list.getItems();
if (files.isEmpty()) {
return create ? createSubFolder(location, name) : null;
}
return files.get(0).getId();
} catch (IOException e) {
// Eat the exception here
Log.w("DriveCloudHelper", e);
}
return create ? createSubFolder(location, name) : null;
}
And here's the stacktrace:
com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:222)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.getIdFromFolder(DriveCloudHelper.java:269)
at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.uploadDatabaseToCloud(DriveCloudHelper.java:405)
at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.synchFileToCloud(DriveCloudHelper.java:368)
at net.wishfullthinking.groceryhelper.interfaces.CloudHelper.mergeCloudStoreCollection(CloudHelper.java:472)
at net.wishfullthinking.groceryhelper.interfaces.CloudHelper.access$10(CloudHelper.java:454)
at net.wishfullthinking.groceryhelper.interfaces.CloudHelper$4.run(CloudHelper.java:444)
at java.lang.Thread.run(Thread.java:856)
Caused by: com.google.android.gms.auth.UserRecoverableAuthException: AppDownloadRequired
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
... 11 more
Try using mContext.getApplicationContext()
in your GoogleAccountCredential.usingOAuth2
call rather than mContext
as that seems to have fixed similar issues elsewhere.
这篇关于驱动器的SDK为Android AuthIntent为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!