【问题标题】:How to properly use Google Drive Rest API?如何正确使用 Google Drive Rest API?
【发布时间】:2019-05-30 14:49:48
【问题描述】:

我有一个应用程序,我想将 SQLite 数据库文件上传到用户的 google drive 帐户。

由于 Google Android API 将停止工作,我想实现 REST API,实现 REST API 需要身份验证。我正在使用 Google 登录,但 REST API 需要某种类型的访问令牌。 如何获得?

我在开发者控制台中创建了一个帐户,启用了 Drive API,并添加了用于 drive/auth 的 Scope。

我使用官方文档https://developers.google.com/identity/sign-in/android/sign-in实现了登录功能

使用此代码,我可以获取用户的电子邮件并创建 DriveService,

    GoogleSignInOptions googleSignInOptions = new 
    GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(DriveScopes.DRIVE))
                .requestEmail()
                .build();

    googleSignInClient = GoogleSignIn.getClient(this, googleSignInOptions);

    Intent googleSignInIntent = googleSignInClient.getSignInIntent();
    startActivityForResult(googleSignInIntent, REQUEST_CODE_SIGN_IN);

在活动结果中,我可以获取用户帐户,使用我创建 DriveServiceHelper 的帐户

    mDriveServiceHelper = new DriveServiceHelper(getGoogleDriveService(getApplicationContext(), googleSignInAccount, "appName"));

现在问题是当我尝试创建文件夹时,它显示了一个关于 (UserRecoverableAuthIOException) 的错误,

查看我用来创建文件夹的代码

public Task<GoogleDriveFileHolder> createFolder(final String folderName, @Nullable final String folderId) {
        return Tasks.call(mExecutor, new Callable<GoogleDriveFileHolder>() {
            @Override
            public GoogleDriveFileHolder call() {
                GoogleDriveFileHolder googleDriveFileHolder = new GoogleDriveFileHolder();

                List<String> root;
                if (folderId == null) {
                    root = Collections.singletonList("root");
                } else {

                    root = Collections.singletonList(folderId);
                }
                File metadata = new File()
                        .setParents(root)
                        .setMimeType(DriveFolder.MIME_TYPE)
                        .setName(folderName);

                File googleFile = null;
                googleFile = mDriveService.files().create(metadata).execute();
                googleDriveFileHolder.setId(googleFile.getId());
                return googleDriveFileHolder;
            }
        });
    }

我尝试使用 StackOverflow 中提供的各种讨论线程来解决错误,但都没有奏效,谁能指导我完成这个?

【问题讨论】:

    标签: android rest google-drive-api


    【解决方案1】:

    给定android.accounts.Account,您可以使用AccountManager.blockingGetAuthToken,将此令牌设置为GoogleCredential,然后将其作为HttpRequestInitializer 传递给Drive.Builder

    String token = AccountManager.get(context)
        .blockingGetAuthToken(<android.accounts.Account>, "oauth2:" + DriveScopes.DRIVE, false);
    GoogleCredential credential = new GoogleCredential().setAccessToken(token);
    Drive service = new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
        .setApplicationName("app name")
        .build();
    

    这个令牌最终会过期,此时您需要调用AccountManager.invalidateAuthToken 并获得一个新的

    如果您已包含 play-services-auth 库,则可以改为传递此凭据:

    GoogleAccountCredential credential = GoogleAccountCredential
        .usingOAuth2(context, Collections.singleton(DriveScopes.DRIVE))
        .setSelectedAccount(googleAccount.getAccount());
    

    【讨论】:

    • 我会尝试您提供的建议并更新您。
    猜你喜欢
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2020-05-02
    相关资源
    最近更新 更多