我尝试了很多解决方案,但我仍然无法连接到公共文件夹。
请给我一个实际工作示例的链接,以使用公钥访问公用文件夹。
目前尚不清楚,光盘只能通过OAuth
或使用公钥连接?
在控制台中,"Deive API"
已启用。
该项目已创建。
生成私钥。
触发连接后"GoogleApiClient.OnConnectionFailedListener"
清单
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<meta-data
android:name="com.google.android.apps.drive.APP_ID"
android:value="id=AIzaSyAXbNLlQ_M"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
GoogleUtil
import android.app.Activity;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveFolder;
public class GoogleDiskUtil implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private final static String LOG_TAG = "CLASS_GOOGLE_DISK_UTIL";
public static final String EXISTING_FOLDER_ID = "0B_SGgMtbKW-efktGZUNjWVI3cFU1anFzUTFFS2F3Y1pfOVZlYk9VUnpQcDNRd2pEOWp2TDA";
private static GoogleApiClient mGoogleApiClient;
private Activity activity;
public GoogleDiskUtil(Activity activity) {
this.activity = activity;
registerDrive();
Drive.DriveApi.fetchDriveId(mGoogleApiClient, EXISTING_FOLDER_ID)
.setResultCallback(getFolderCallback);
}
private void registerDrive() {
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
// .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
final private ResultCallback<DriveApi.DriveIdResult> getFolderCallback = new ResultCallback<DriveApi.DriveIdResult>() {
@Override
public void onResult(DriveApi.DriveIdResult result) {
if (!result.getStatus().isSuccess()) {
Toast.makeText(activity, "Cannot find DriveId. Are you authorized to view this file?", Toast.LENGTH_SHORT).show();
return;
}
DriveFolder folder = Drive.DriveApi.getFolder(mGoogleApiClient, result.getDriveId());
folder.listChildren(mGoogleApiClient)
.setResultCallback(metadataResult);
}
};
final private ResultCallback<DriveApi.MetadataBufferResult> metadataResult = new
ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
Toast.makeText(activity, "Problem while retrieving files", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(activity, "Successfully listed files.", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onConnected(Bundle bundle) {
Log.i(LOG_TAG, "GoogleApiClient connected");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(LOG_TAG, "GoogleApiClient connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(LOG_TAG, "GoogleApiClient connection failed: " + connectionResult.toString());
if (!connectionResult.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), activity, 0).show();
return;
}
try {
Log.i(LOG_TAG, "trying to resolve the Connection failed error...");
connectionResult.startResolutionForResult(activity, 1);
} catch (IntentSender.SendIntentException e) {
Log.e(LOG_TAG, "Exception while starting resolution activity", e);
}
}
}
感谢。