如何使用GoogleApiClient获取GoogleDrive配额(剩余可用尺寸)?

时间:2016-01-16 17:02:39

标签: android google-drive-api google-drive-android-api quota

我在Android应用上只使用GoogleApiClient。 我无法获得我的GoogleDrive配额使用情况(总计/可用/等等)。 作为谷歌搜索...,可能是DriveService(com.google.api.services.drive.Drive)是可能的......但我不是那样......

如何获取GoogleDrive配额信息?

(注)这是我的GoogleApiClient构建器。

GDApi = new GoogleApiClient.Builder(mContext)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .setAccountName(token)
                .build();

1 个答案:

答案 0 :(得分:0)

不幸的是(AFAIK),GDAA中没有getQuotaBytesTotal(), getQuotaBytesUsed()的等价物。

因此,获取此信息(以及其他不存在的功能 - 如thumbnails,...)的唯一方法是添加REST Api。不过,请确保您仔细使用此混音,可能会遇到许多延迟/时间问题。

实现这一目标有一些先决条件:

1 /确保在项目中包含以下jar:

compile 'com.google.apis:google-api-services-drive:v2-rev105-1.17.0-rc'
compile 'com.google.http-client:google-http-client-gson:1.20.0'
compile 'com.google.api-client:google-api-client-android:1.20.0'
...
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.json.gson.GsonFactory;

2 /您需要在清单中另外获得许可:

  <uses-permission android:name="android.permission.GET_ACCOUNTS" />

3 /您必须获得com.google.api.services.drive.Drive服务并在GDAA的onConnected()回调后进行查询,如下所示:

private static GoogleApiClient mGAC;
private static com.google.api.services.drive.Drive mGooSvc;

static boolean init(Activity context) {
  if (context != null) try {

    mGooSvc = new com.google.api.services.drive.Drive.Builder(
      AndroidHttp.newCompatibleTransport(),
      new GsonFactory(),
      GoogleAccountCredential.usingOAuth2(
        context.getApplicationContext(),
        Collections.singletonList(com.google.api.services.drive.DriveScopes.DRIVE_FILE)
      )
    ).build();

    mGAC = new GoogleApiClient.Builder(context)
      .addApi(Drive.API)
      // ... additional APIs
      .addScope(Drive.SCOPE_FILE)
      .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnectionSuspended(int i) {
        }

        @Override
        public void onConnected(Bundle bundle) {
          new Thread(new Runnable() {
            @Override
            public void run() {
              try {
                com.google.api.services.drive.model.About about = mGooSvc.about().get().execute();
                System.out.println("Total quota (bytes): " + about.getQuotaBytesTotal());
                System.out.println("Used quota (bytes): " + about.getQuotaBytesUsed());
              } catch (Exception e) { e.printStackTrace(); }
              //} catch (UserRecoverableAuthIOException uraIOEx) {
              //  // standard authorization failure - user fixable
              //} catch (GoogleAuthIOException gaIOEx) {
              //  // usually PackageName /SHA1 mismatch in DevConsole
              //} catch (IOException e) {
              //  if (e instanceof GoogleJsonResponseException) {
              //    if (404 == ((GoogleJsonResponseException) e).getStatusCode()) {
              //      // '404 not found' in FILE scope, consider connected
              //    }
              //  }
              //} catch (Exception e) {
              //  // "the name must not be empty" indicate
              //  // UNREGISTERED / EMPTY account in 'setSelectedAccountName()' above
              //}
            }
          }).start();
        }
      })
      .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult rslt) {
          // perform standard authorization dance
        }
      })
      .build();

    mGAC.connect();

    return true;
    } catch (Exception e) {e.printStackTrace();}
  return false;
}

请注意,我注释掉了REST execute()方法的错误/授权处理。这是因为GDAA connect()/onConnected()/onConnectionFailed()已经处理了授权(如果范围相同)。

祝你好运