授权我的应用程序使用现有文件夹 [英] Authorizing my application to use an Existing Folder

查看:23
本文介绍了授权我的应用程序使用现有文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Android Drive API,尝试连接时使用:

Using the Android Drive API, when trying to connect using:

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API).addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();

getGoogleApiClient.connect();

我得到选择一个帐户屏幕,选择一个帐户后,我看到:

I get the select an account screen and after selecting an account, I see this:

查看和管理您打开或创建的 Google 云端硬盘文件用这个应用程序

View and manage Google Drive files that you have opened or created with this app

如果我要访问其他应用程序创建的文件怎么办?

What if I what to access files created by other apps?

我的应用程序是否可以请求特定文件夹的授权?

Is there a way for my application to ask for authorization of a specific folder?

推荐答案

Google Play 服务客户端库为 Android 开发人员提供 API,以便与各个 Google 服务无缝集成.

Google Play Services client library provides an Android developer with APIs for seamless integration with the individual Google services.

该库还允许您的应用提供一致的用户界面,以便从用户那里获得授权,以使用他们的凭据访问这些服务.

The library also let your apps offers a consistent user interface to obtain authorization from users to access these services with their credentials.

访问 Google 云端硬盘服务

Google Play 服务客户端库附带 Google Drive Android API 提供对 Google Drive 服务的访问.

Google Play services client library ships with Google Drive Android API that provides access to the Google Drive service.

但是,Google Drive Android API 目前仅提供 drive.filedrive.appfolder 授权范围 如果您的应用需要访问用户驱动器上的所有内容,则在某种程度上会受到限制.

However, Google Drive Android API currently offers only drive.file and drive.appfolder authorization scopes which are limited in a way if your app needs access to all the contents on the user's drive.

要提供驱动范围,您需要使用 Java 版 Drive API 客户端库.此库依赖于 适用于 Java 的 Google API 客户端库 提供生成的客户端库,以便使用来自任何应用程序(Web、已安装或 Android)的 REST API 访问单个 Google 服务

To provide drive scope, you will need to use REST APIs offered by Drive API Client Library for Java. This library has a dependency on Google APIs Client Library for Java that offers generated client libs for access to individual Google services using REST APIs from any application (web, installed or Android)

下载适用于 Java 的 Drive API v2 客户端库.libs 文件夹包含所有应用程序类型(Web、已安装或 Android 应用程序)可能需要的所有全局适用的依赖项(Java 的 Google API 客户端库).

Download the Drive API v2 Client Library for Java. The libs folder contains all the globally-applicable dependencies (Google APIs Client Library for Java) you might need across all application types (web, installed, or Android application).

对于 Android,您将需要以下 jars(如 drive/readme.html 中所述)来整理依赖关系

For Android, you will need the following jars (as described in drive/readme.html) to sort out the dependencies

google-api-services-drive-v2-rev143-1.19.0.jar
google-api-client-1.19.0.jar
google-api-client-android-1.19.0.jar
google-http-client-1.19.0.jar
google-http-client-android-1.19.0.jar
google-http-client-gson-1.19.0.jar
google-oauth-client-1.19.0.jar
gson-2.1.jar
jsr305-1.3.9.jar

授权

在访问 Google Drive(或任何其他 Google)服务之前,您需要授权您的应用程序(使用 OAuth 2.0)

Before accessing Google Drive (or any other Google) service, you need to authorize your application (using OAuth 2.0)

  1. 对于使用 Google Play 服务客户端库的应用程序,这将由 GoogleApiClient.

  1. For applications using Google Play Services client library, this will be handled by GoogleApiClient.

但是,如果您要使用的服务/Google API 未包含在 Google Play 服务库中,您可以使用适当的 REST API 手动发出请求或使用服务提供商提供的客户端库,但您必须获取 OAuth 2.0 令牌.

However, if the service/Google API you want to use is not included in the Google Play services library, you can connect using the appropriate REST API to manually make requests or using a client library provided by the service provider, but you must obtain an OAuth 2.0 token.

对于 drive 范围,您需要使用 Google APIs Client Library for Java 和生成的客户端库(在我们的例子中是 drive v2).要获取授权令牌,您可以 -

For drive scope, you need to use Google APIs Client Library for Java and the generated client libraries (drive v2, in our case). To obtain an authorization token, you can either -

  • 直接使用 OAuth 2.0 库来自 适用于 Java 的 Google API 客户端库(不适用于 android)
  • 或通过 GoogleAuthUtil 和 AccountPicker.阅读使用 Google 授权 REST API.(GoogleAuthUtil.getToken() 缓存和管理令牌到期并自行刷新.但是,在网络错误/服务器负载的情况下,您可能需要在重试令牌之前使用指数退避算法,以免请求淹没服务器. 参考3)
  • 或使用 GoogleAccountCredentialGoogle API 客户端库附带的 google-api-client-android-1.19.0.jar 中定义对于 Java.该软件包提供了基于 Google Play 服务GoogleAccountCredential 只是 GoogleAuthUtilAccountPicker<的包装器/em>.这将允许您使用相同的一致授权流程和 Google Play 服务客户端库随附的标准帐户选择器 UI,同时将令牌管理和使用指数回退策略(如上所述)委托给 GoogleAccountCredential.参考 1 示例.
  • directly use the OAuth 2.0 library from Google APIs Client Library for Java (not preferred for android)
  • or leverage the authorization portion of the Google Play services library using GoogleAuthUtil and AccountPicker. Read Authorizing with Google for REST APIs. (GoogleAuthUtil.getToken() caches and manages token expiry and refresh itself. However, in case of network errors/server load, you might need to use an exponential back-off algorithm before retrying for the token so to not flood the server with requests. Refer 3)
  • or use the GoogleAccountCredential defined in google-api-client-android-1.19.0.jar that comes with Google APIs Client Library for Java. The package offers Utilities based on Google Play services and GoogleAccountCredential is just a wrapper around GoogleAuthUtil and AccountPicker. This would allow you to use the same consistent authorization flow and the standard account picker UI that comes with Google Play services client library while delegating the token management and using an exponential back-off strategy (as noted above) to GoogleAccountCredential. Refer 1 for an example.

请注意,对于最后两种方法,您同时使用 Google Play 服务客户端库(用于授权)和 Google APIs Java 客户端库em> 以及 Drive API v2 Client Library for Java(用于访问具有 drive 身份验证范围的 Google Drive 服务.

Note that with the last two approaches, you are using both Google Play services client libraries (for authorization) and Google APIs Client Library for Java along with Drive API v2 Client Library for Java (for access to Google Drive service with drive authentication scope).

包参考

也结帐-

  1. http://www.techsfo.com/blog/2014/03/android-preparing-to-access-google-drive-with-an-app-part-1/
  2. http://developer.samsung.com/android/technical-docs/Using-Google-APIs
  3. Google Play 服务和 OAuth 身份工具

这个答案也发布在我的博客 这里.

This answer also posted at my blog here.

这篇关于授权我的应用程序使用现有文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆