标签: google-drive-android-api

从Google Drive Android API获取帐户名称/电子邮件

我正在使用新的Google云端硬盘Android API,我可以连接,并执行所有允许我这样做的操作,但为了使用SpreadsheetService,我需要从已登录的用户中提取accountName/email.

我怎么做?

这是我连接Google Drive API的代码.它工作正常.

private void connect() {

    if (isGooglePlayServicesAvailable()) {
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        // And, connect!

        if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
            mGoogleApiClient.connect();
            writeLog("Connecting...");
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我使用SpreadsheetService API的地方,但我需要帐户名来获取令牌.

String accountName = "???"; // how do I get this From GoogleClientApi / mGoogleApiClient ?
String accessToken = GoogleAuthUtil.getTokenWithNotification(GDriveActivity.this, accountName, scope, null);
SpreadsheetService service = new SpreadsheetService("v1");
Run Code Online (Sandbox Code Playgroud)

我试过这个添加这个:

String accountName = …
Run Code Online (Sandbox Code Playgroud)

android google-spreadsheet-api google-drive-android-api

7
推荐指数
2
解决办法
6635
查看次数

Google Drive Android API中DriveId.getResourceId()的不可预知的结果

问题是来自' DriveId.getResourceId() ' 的'resourceID' 在新创建的文件(' DriveFolder.createFile(GAC,meta,cont) '的产品)上不可用(返回NULL ).如果通过常规列表或查询过程检索文件,则'resourceID'是正确的.

我怀疑这是一个时间/延迟问题,但目前尚不清楚是否存在强制刷新的应用程序操作." Drive.DriveApi.requestSync(GAC) "似乎没有任何效果.

google-drive-android-api

7
推荐指数
2
解决办法
3723
查看次数

以编程方式将视频上传到Google云端硬盘(Android API)

我已按照云端硬盘API指南(https://developer.android.com/google/play-services/drive.html)操作,我的应用现在可以顺利上传照片,但我现在正试图上传视频(mp4)但未成功.

有谁知道如何实现这一目标?视频是一个新生成的mp4文件,我有路径存储在设备上.

对于图片,它是这样做的:

Drive.DriveApi.newDriveContents(mDriveClient).setResultCallback(
    new ResultCallback<DriveContentsResult>() {

@Override
public void onResult(DriveContentsResult result) {
    if (!result.getStatus().isSuccess()) {
        Log.i(TAG, "Failed to create new contents.");
        return;
    }
    OutputStream outputStream = result.getDriveContents().getOutputStream();
    // Write the bitmap data from it.
    ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 80, bitmapStream);
    try {
        outputStream.write(bitmapStream.toByteArray());
    } catch (IOException e1) {
        Log.i(TAG, "Unable to write file contents.");
    }
    image.recycle();
    outputStream = null;
    String title = Shared.getOutputMediaFile(Shared.MEDIA_TYPE_IMAGE).getName();
    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
        .setMimeType("image/jpeg").setTitle(title)
        .build();
    Log.i(TAG, "Creating new pic on …
Run Code Online (Sandbox Code Playgroud)

android google-drive-android-api

7
推荐指数
1
解决办法
1万
查看次数

如何清除GoogleApiClient默认帐户和凭据

我连接GoogleApiClient以用于Google云端硬盘.我像这样构建客户端:

        GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
Run Code Online (Sandbox Code Playgroud)

我的经验是,一次为此客户端发出连接请求时,会显示AccountPicker对话框并显示Google云端硬盘的同意屏幕.如果用户选择一个帐户,同意并且连接成功完成,则AccountManager或某些相关功能会将所选帐户保存为默认帐户,并将驱动器范围的凭据(OAuth令牌?)保存.在后续连接请求中,为了方便用户,使用保存的值,并且用户没有看到用于帐户选择或同意的UI.

对于开发测试,我希望能够清除默认帐户和保存的凭据,以便我可以执行连接失败解决处理.我还没有办法做到这一点.我尝试了这个没有成功:

String driveScope = "https://www.googleapis.com/auth/drive.file";
String tokenType = "oauth2:" + driveScope;

AccountManager.get(this).invalidateAuthToken(
    GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE, tokenType);
Run Code Online (Sandbox Code Playgroud)

android google-play-services google-drive-android-api

7
推荐指数
1
解决办法
5011
查看次数

登录到Android上的谷歌驱动器,而无需将谷歌帐户添加到设备

我开发了一个Android应用程序,需要允许对用户的谷歌驱动器进行读/写访问.此外,它应该在公共设备上运行(意味着许多人可能使用相同的设备访问他们的谷歌驱动器帐户).在这种情况下,将每个用户的帐户添加到Android帐户管理器是不可接受的.不幸的是,所有官方指南都使用在设备上注册的Google帐户进行身份验证.将显示从设备中选择现有Google帐户或添加新帐户的弹出窗口.

protected void onStart() {
        super.onStart();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                            // Optionally, add additional APIs and scopes if required.
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        mGoogleApiClient.connect();
    }



    /**
     * Called when {@code mGoogleApiClient} is trying to connect but failed.
     * Handle {@code result.getResolution()} if there is a resolution
     * available.
     */
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // Show a localized error dialog.
            GooglePlayServicesUtil.getErrorDialog(
                    result.getErrorCode(), …
Run Code Online (Sandbox Code Playgroud)

android google-drive-api google-play-services google-drive-android-api

7
推荐指数
1
解决办法
5973
查看次数

将SQLlite数据库备份/还原到Google云端硬盘应用文件夹

我正在尝试将功能合并到备份和恢复应用程序数据库到谷歌驱动器应用程序文件夹(用户不可见).我浏览了为android 提供的基本设置指南设置指南,可以让用户授权应用程序并到达调用onConnected方法的位置.

我面临的问题是,我不知道如何将数据库文件(.db)从设备"发送"到google drive app文件夹.Google已经共享了用于创建新文件的代码段,但就此而言.我确实发现了一个前面提到的问题,模糊地类似于我正在寻找的Google驱动器备份和恢复Android应用程序的数据库和共享首选项,但后来又不是我想要的.

搜索谷歌不会返回任何有用的链接,因为这是相对较新的API.

更新1:

共享onConnected()代码,

    public void onConnected(Bundle bundle) {
    Toast.makeText(GoogleSignIn.this, "In onConnected activity", Toast.LENGTH_SHORT).show();

    // Testing to see if database is indeed uploaded to google drive app folder
    String db_name = "XXX_DB";
    String currentDBPath = "/data/" + "com.abc.efg" + "/databases/" + db_name;
    saveToDrive(
            Drive.DriveApi.getAppFolder(mGoogleApiClientDrive),
            "XXX_DB.db",
            "application/x-sqlite3",
            new java.io.File(currentDBPath)
    );
}
Run Code Online (Sandbox Code Playgroud)

更新2:

下面分享的解决方案非常适合将数据库上传到google drive app文件夹.对于那些可能面临类似问题的人,请尝试将数据库路径更改为"/data/data/com.abc.efg/databases/" + db_name而不是"/data/com.abc.efg/databases/" + db_name

下一步是能够从google drive app文件夹中检索和恢复数据库.如果我能够使它工作,应该更新这个问题.

android android-sqlite google-drive-android-api

7
推荐指数
1
解决办法
6823
查看次数

使用Google云端硬盘sdk的Play商店应用始终显示"选择帐户"弹出窗口

我已将Google驱动程序sdk与应用程序集成,并且可以正常运行调试和签名版本.但是,当从Play商店安装应用程序时,它始终显示"帐户选择器"弹出窗口.似乎认证对于Play商店构建失败了.

应用程序的软件包名称和SHA-1签名证书指纹已添加到控制台中.

在驱动器sdk集成期间有人遇到了同样的问题吗?

android drive google-drive-api google-play-services google-drive-android-api

7
推荐指数
1
解决办法
171
查看次数

Google云端硬盘Android Api ChangeEvent侦听器工作方式

我正在滥用这个论坛来征求GDAA通知功能的预期用途的解释(来自GDAA支持团队).这里的文档足以实现它,并且两个相关的SO问题2298049722778501涉及相同的主题.但我无法理解它的用途.这是一个例子.

  • 设备A'触摸'Drive对象并立即获得通知.
  • 设备B'触摸'相同的Drive对象,并且设备A在" 稍后某个时间 " 得到通知,即使两个设备都在wifi上.

那么,有什么好处呢?某个特定设备上的应用程序知道它只是修改了一个对象,不需要通知.另一方面,如果它通知另一个设备修改了具有CONSIDERABLE(分钟)延迟的对象,那么如何实现同步呢?或者如何使用呢?我被建议使用'requestSync()',但没有看到任何差异(此外,它与直接轮询有何不同).
今天,我可以通过轮询可靠地同步多个设备,但无法使其与未按时通知的通知一起使用.

google-drive-android-api

6
推荐指数
0
解决办法
410
查看次数

gapi.auth2.getAuthInstance().isSignedIn.listen() 不起作用

我在谷歌驱动器 api 身份验证中使用 OAuth2.0。我有一个作为回调的isSignedIn监听器afterSignIn
我的问题:登录后,afterSignIn()功能未触发。有人知道如何解决这个问题吗?

function googleDriveAuthentication($rootScope){
    var API_KEY = '...'
    var CLIENT_ID = '...';
    var SCOPES = 'https://www.googleapis.com/auth/drive';
    this.authenticate = function(){
        gapi.load('client:auth2',authorize);
    }
    function authorize(){
        gapi.client.setApiKey(API_KEY);
        gapi.auth2.init({
            client_id: CLIENT_ID,
            scope: SCOPES
        }).then(function(authResult){
            var auth2 = gapi.auth2.getAuthInstance();
            auth2.isSignedIn.listen(afterSignIn);
            auth2.signIn();
        });
    }
    function afterSignIn(){
        console.log('authenticated successfully');
        $rootScope.authenticated = true;
        $rootScope.$broadcast('authenticated');
        gapi.client.load('drive', 'v3');
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript google-oauth google-drive-android-api

6
推荐指数
1
解决办法
4188
查看次数

使用 GDrive.files.list({)) 时找不到共享驱动器

我想通过使用react-native-google-drive-api-wrapper库从共享驱动器获取文件列表,但我收到错误。这是我的代码。

GoogleSignin.configure({
  scopes: [
  'https://www.googleapis.com/auth/drive',
  'https://www.googleapis.com/auth/drive.file',
],
      webClientId:"xxxxxxx",
      offlineAccess: true,
      //forceConsentPrompt: true,
 })
    await GoogleSignin.getTokens().then(res=>GDrive.setAccessToken(res.accessToken))
          GDrive.init()
          await GDrive.files.list({
            q: "mimeType='application/pdf'",
            fieIds:'*',
            corpora:'drive',
            supportsAllDrives:true,
            includeItemsFromAllDrives:true,
            driveId:'1YAyXfzIAOP5TejUW00RR9jhXaBIXKF8_'
          })
           .then(res=>res.json())
           .then(data=>console.log(data))
           .catch(err=>console.log(err))
Run Code Online (Sandbox Code Playgroud)

我收到这个错误,谢谢您的帮助。

{"error": {"code": 404, "errors": [[Object]], "message": "Shared drive not found: 1YAyXfzIAOP5TejUW00RR9jhXaBIXKF8_"}}
Run Code Online (Sandbox Code Playgroud)

已修复 我提供了folderId而不是driveId,因此您必须像这样更改查询:

GDrive.init()
       GDrive.files.list({
       q: "' folderId ' in parents",
      })
       .then(res=>res.json())
       .then(data=>console.log(data))
       .catch(err=>console.log(err))
Run Code Online (Sandbox Code Playgroud)

google-drive-android-api react-native

6
推荐指数
1
解决办法
4646
查看次数