Android 安卓&x2014;Google Drive API使用公钥获取公用文件夹文件?

Android 安卓&x2014;Google Drive API使用公钥获取公用文件夹文件?,android,google-api,google-drive-api,Android,Google Api,Google Drive Api,我尝试了许多解决方案,但仍然无法连接到公用文件夹 请给我一个使用公钥访问公用文件夹的实际工作示例的链接 不清楚,只能通过OAuth或使用公钥连接光盘 在控制台中,启用了“Deive API” 该项目已创建 生成私钥 触发连接后“GoogleAppClient.OnConnectionFailedListener” 清单 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <

我尝试了许多解决方案,但仍然无法连接到公用文件夹

  • 请给我一个使用公钥访问公用文件夹的实际工作示例的链接

  • 不清楚,只能通过
    OAuth
    或使用公钥连接光盘

  • 在控制台中,启用了“Deive API”

    该项目已创建

    生成私钥

    触发连接后
    “GoogleAppClient.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>
    
    
    
    谷歌工具

    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);
            }
        }
    }
    
    导入android.app.Activity;
    导入android.content.IntentSender;
    导入android.os.Bundle;
    导入android.util.Log;
    导入android.widget.Toast;
    导入com.google.android.gms.common.ConnectionResult;
    导入com.google.android.gms.common.GooglePlayServicesUtil;
    导入com.google.android.gms.common.api.GoogleAppClient;
    导入com.google.android.gms.common.api.ResultCallback;
    导入com.google.android.gms.drive.drive;
    导入com.google.android.gms.drive.DriveApi;
    导入com.google.android.gms.drive.DriveFolder;
    公共类GoogleDiskUtil实现GoogleAppClient.ConnectionCallbacks,
    GoogleAppClient.OnConnectionFailedListener{
    私有最终静态字符串LOG\u TAG=“CLASS\u GOOGLE\u DISK\u UTIL”;
    公共静态最终字符串现有\u文件夹\u ID=“0B\u SGgMtbKW-efktgzunjwvi3cfu1anfzutfs2f3y1pfovzlyk9vunpqcdnrd2peowp2tda”;
    私有静态GoogleapClient MGoogleapClient;
    私人活动;
    公共Google Diskutil(活动){
    这个。活动=活动;
    registerDrive();
    Drive.DriveApi.fetchDriveId(mGoogleApiClient,现有的\u文件夹\u ID)
    .setResultCallback(getFolderCallback);
    }
    私有无效注册表驱动程序(){
    if(mGoogleApiClient==null){
    mgoogleapclient=新的Googleapclient.Builder(活动)
    .addApi(Drive.API)
    .addScope(驱动器.SCOPE\u文件)
    //.addScope(Drive.SCOPE\u APPFOLDER)//应用程序文件夹示例需要
    .addConnectionCallbacks(此)
    .addOnConnectionFailedListener(此)
    .build();
    }
    mGoogleApiClient.connect();
    }
    最终私有ResultCallback getFolderCallback=新ResultCallback(){
    @凌驾
    public void onResult(DriveApi.DriveIdResult结果){
    如果(!result.getStatus().issucess()){
    Toast.makeText(活动“找不到DriveId。您有权查看此文件吗?”,Toast.LENGTH\u SHORT.show();
    返回;
    }
    DriveFolder folder=Drive.DriveApi.getFolder(mGoogleApiClient,result.getDriveId());
    folder.listChildren(mgoogleAppClient)
    .setResultCallback(metadataResult);
    }
    };
    最终私有结果Callback metadataResult=new
    ResultCallback(){
    @凌驾
    public void onResult(DriveApi.MetadataBufferResult结果){
    如果(!result.getStatus().issucess()){
    Toast.makeText(活动,“检索文件时出现问题”,Toast.LENGTH_SHORT.show();
    返回;
    }
    Toast.makeText(活动,“成功列出的文件”,Toast.LENGTH_SHORT).show();
    }
    };
    @凌驾
    未连接的公共空间(捆绑包){
    Log.i(Log_标签,“GoogleAppClient已连接”);
    }
    @凌驾
    公共空间连接暂停(int i){
    Log.i(Log_标签,“GoogleAppClient连接已挂起”);
    }
    @凌驾
    公共无效onConnectionFailed(ConnectionResult ConnectionResult){
    Log.i(Log_标记,“GoogleAppClient连接失败:”+connectionResult.toString());
    如果(!connectionResult.hasResolution()){
    GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(),活动,0.show();
    返回;
    }
    试一试{
    Log.i(Log_标记,“尝试解决连接失败错误…”);
    connectionResult.StartResult解决方案(活动,1);
    }catch(IntentSender.sendtintentexe){
    Log.e(Log_标签,“启动解析活动时异常”,e);
    }
    }
    }
    

    谢谢。

    Google Drive for Android仅支持当前用户的授权,即驱动器中的文件。我认为这不包括与您共享的文件,更不用说公共文件夹了。您必须使用访问公用文件夹。Google Drive for Android仅支持当前用户的授权,即驱动器中的文件。我认为这不包括与您共享的文件,更不用说公共文件夹了。您必须使用访问公用文件夹。