在Google帐户上签名是成功的,但是当我尝试在google驱动器上获取文件时总是失败并显示错误,显示如下“com.google.android.gms.common.api.ApiException:8:API失败恢复时连接“。我也看过这个链接enter link description here。但是没有解决方案。这是我的代码。我在谷歌控制台上注册了我的应用程序。
的摇篮
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.pripun.application"
minSdkVersion 21
targetSdkVersion 21
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.google.android.gms:play-services-auth:11.8.0'
compile 'com.google.android.gms:play-services-drive:11.8.0'
implementation 'com.google.firebase:firebase-auth:11.8.0'
}
的AndroidManifest.xml
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:name=".GoogleDr" android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
GoogleDR.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveClient;
import com.google.android.gms.drive.DriveResourceClient;
import com.google.android.gms.drive.MetadataBuffer;
import com.google.android.gms.drive.query.Filters;
import com.google.android.gms.drive.query.Query;
import com.google.android.gms.drive.query.SearchableField;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
/**
* Created by Akbar on 3/22/2018.
*/
public class GoogleDr extends AppCompatActivity {
private GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN = 7;
private String TAG = "Hasil SIGN";
private static final int REQUEST_CODE_SIGN_IN = 0;
private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
private static final int REQUEST_CODE_CREATOR = 2;
private DriveClient mDriveClient;
private DriveResourceClient mDriveResourceClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googledr);
signIn();
}
private void signIn() {
GoogleSignInClient GoogleSignInClient = buildGoogleSignInClient();
startActivityForResult(GoogleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
}
private GoogleSignInClient buildGoogleSignInClient() {
GoogleSignInOptions signInOptions =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.requestScopes(Drive.SCOPE_APPFOLDER)
.build();
return GoogleSignIn.getClient(this, signInOptions);
}
private void handleSignInResult(GoogleSignInResult result) {
android.util.Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
android.util.Log.e(TAG, "display name: " + acct.getDisplayName());
String personName = acct.getDisplayName();
String personPhotoUrl = acct.getPhotoUrl().toString();
String email = acct.getEmail();
Toast.makeText(getApplicationContext(), "Name: " + personName + ", email: " + email
+ ", Image: " + personPhotoUrl,Toast.LENGTH_LONG).show();
updateUI(true);
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
private void updateUI(boolean bl){
Toast.makeText(getApplicationContext(),bl+"",Toast.LENGTH_LONG).show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);'
//Toast.makeText(getApplicationContext(),requestCode+"",Toast.LENGTH_LONG).show();;
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(this);
handleSignInResult(result);
if (requestCode == RC_SIGN_IN) {
result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
mDriveClient = Drive.getDriveClient(this,lastSignedInAccount);
// Build a drive resource client.
mDriveResourceClient =
Drive.getDriveResourceClient(this, lastSignedInAccount);
fileslist();
}
public void fileslist(){
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.TITLE, "example.txt"))
.build();
Task<MetadataBuffer> queryTask = getDriveResourceClient().query(query);
// [END query_files]
// [START query_results]
queryTask
.addOnSuccessListener(this,
new OnSuccessListener<MetadataBuffer>() {
@Override
public void onSuccess(MetadataBuffer metadataBuffer) {
Toast.makeText(getApplicationContext(),"ISO",Toast.LENGTH_LONG).show();;
Log.d("SUCCESS","");
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"ORA",Toast.LENGTH_LONG).show();;
// Handle failure...
// [START_EXCLUDE]
Log.d("FAIL"+e.toString(),"");
//showMessage(getString(R.string.query_failed));
finish();
// [END_EXCLUDE]
}
});
}
private DriveResourceClient getDriveResourceClient(){
return mDriveResourceClient;
}
}
答案 0 :(得分:0)
您的模拟器应安装最新的Parallel.For
。否则使用真正的Android设备进行测试。
答案 1 :(得分:0)
您可以尝试使用此github forum建议的解决方案,即在您的Google开发者控制台中启用Drive API。
您可以在Java Quickstart for Drive API中阅读更多此设置。