如何使用Android API和特定帐户连接到Google云端硬盘? [英] How to connect to Google Drive using the Android API and a specific account?

查看:128
本文介绍了如何使用Android API和特定帐户连接到Google云端硬盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Google云端硬盘进行云同步.

是否可以使用 Google Drive API (针对Android)连接到将用作云存储服务器的特定帐户?

当我尝试与指定的Google帐户建立联系时,如何设置用户帐户和密码?

到目前为止,我正在使用:

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

但这为用户提供了一个登录对话框,以选择自己的本地帐户,这不是我想要的.谢谢.

解决方案

这是此处找到有关此常规"过程的更多具体信息. /p>

棘手的步骤是获取刷新令牌,因为一旦将其存储在安全的位置,就可以使用简单的httpRequests进行任何操作.

要获取刷新令牌,用户必须同意授予您的项目访问某些功能的权限.这在Google语言中称为scopes.您可以通过多种不同方式(根据Google)来做到这一点:

发出此请求的方法有几种,并且根据 您正在构建的应用程序的类型.例如,JavaScript 应用程序可能会使用浏览器重定向到来请求访问令牌 Google,而在没有浏览器的设备上安装的应用程序 使用Web服务请求.

他们没有告诉您的一件事是,在您的情况下,使用哪种介质获取refresh_token都没有关系,因为您只使用一个帐户. Google云端硬盘API并非真正设计成可以像服务器一样使用.它旨在供希望在其每个用户帐户上存储信息的开发人员使用.就像您拥有图片应用程序一样,您可以使用一项功能,将经过编辑的图片存储在某人的个人Google云端硬盘帐户中.

您(以及最近询问过的许多其他人)想要做的基本上是将云端硬盘帐户用作服务器.这意味着您获取refresh_token的介质不必与您使用驱动器帐户的介质相关.

例如,在我的特定情况下,我想要一种将用户图片免费存储在服务器上的Android应用程序的方法.我正在使用名为Parse的免费服务充当我的数据库服务器,但它们在您的免费层中为您提供了非常有限的文件存储.

因此,我决定尝试弄清楚如何使用Google云端硬盘帐户来扩展我的存储空间.我创建了一个通用的Gmail帐户(例如"hostingaccount@gmail.com")作为文件的主机(免费提供15g).

要获取我的refresh_token,我设置了一个php服务器来验证该帐户.当有人进入我设置的页面时,系统会提示他们登录,然后授予对我的项目的访问权限,以使用其Google云端硬盘帐户(特定范围:https://www.googleapis.com/auth/drive).然后,我设置脚本以在屏幕上打印该帐户的refresh_token.当我现在可以轻松地将httpRequest发送到:

时,我将该字符串复制并放入了服务器中.

https://www.googleapis.com/oauth2/v3/token/获取访问令牌并访问:

https://www.googleapis.com/upload/drive/v2/files上传文件.

我将其链接到此编辑的顶部,但 解决方案

This is an excerpt from this answer I gave on a similar question:

There are two main items you need to make a request to a Google API:

  1. refresh_token - Used to get more access_tokens and never expires
  2. access_token - Used to send API calls (example: upload a file) and expires every 60 minutes.

In order to get either of these you have to follow certain steps.

  1. Obtain a OAuth 2.0 client ID through the Google Developers Console

  2. Obtain an access token from the Google Authorization Server

  3. Send the access token to an API

  4. Refresh the access token, if necessary

More specific information about this "general" process can be found here.

The tricky step is getting the refresh token because once you have that stored in a secure location, you can do anything with simple httpRequests.

To get a refresh token, a user has to agree to give your project access to certain features. This is called scopes in Google language. You can do this in many different ways (according to Google):

There are several ways to make this request, and they vary based on the type of application you are building. For example, a JavaScript application might request an access token using a browser redirect to Google, while an application installed on a device that has no browser uses web service requests.

The one thing they don't tell you is that, in your case, it doesn't matter what medium you use to get the refresh_token because you are only using one account. The Google Drive API isn't really designed to be used like a server. It is designed to be used by a developer that wants to store information on each of its user's accounts. Like if you had a picture app, you could have a feature that stores edited picture on someone's personal Google Drive account.

What you (and many others who have recently asked about) want to do is essentially use a Drive account as a server. That means that the medium through which you get your refresh_token does not have to be related to the medium in which you are using the Drive Account.

For example in my specific case, I wanted a way to store user pictures on a server for free for an android app. I am using this free service called Parse to act as my database server, but they give you very limited file storage in their free tier.

Because of this, I decided to try and figure out how to use Google drive accounts to expand my storage. I created a generic gmail account something like "hostingaccount@gmail.com" to be the host of the files (15g for free).

To get my refresh_token, I setup a php server to authenticate that one account. When someone goes to the page I setup, they are prompted to login and then grant access to my project to use their Google Drive account (Specific scope: https://www.googleapis.com/auth/drive). I then setup the script to print the refresh_token for that account on the screen. I copied that string an put it into my server when now I can easily send httpRequests to:

https://www.googleapis.com/oauth2/v3/token/ to get an access token and to:

https://www.googleapis.com/upload/drive/v2/files to upload files.

I link it at the top of this edit, but this answer shows you how to get a refresh token using my php method. I never spent the time to try an figure out how to get a refresh token any other way, but if you read my whole long answer I think I mention that I believe that this can also be done with the Android Google Drive API.

I have tried to help so many people with this problem, maybe I should just start a blog and make a tutorial about it ;)

这篇关于如何使用Android API和特定帐户连接到Google云端硬盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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