如何设置Google云端硬盘客户端ID以便在没有本地服务器的情况下进行访问? [英] How to set up a Google Drive Client ID for access without a local server?

查看:77
本文介绍了如何设置Google云端硬盘客户端ID以便在没有本地服务器的情况下进行访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在随机计算机上打开 jsfiddle 并登录并验证并使用驱动器API,而不必一直运行本地服务器?究竟该如何设置呢?很抱歉,如果这是一个简单的问题,但我有点迷失了,因为到目前为止我发现的说明尚不清楚.

I'm wondering if it's possible to, say, open up a jsfiddle on a random computer and log in and authenticate and use the drive API, without having to have a local server running all the time? And how exactly does one go about setting this up? I'm sorry if this is a simple question but I'm just sorta lost because the instructions that I've found so far are unclear.

到目前为止,我来自此处

So far I have, following from here and here:

  1. 通过 Google Developers Console 创建了一个项目.
  2. 在那里打开该项目,导航至APIs&进行身份验证,并确保已启用Drive API
  3. 进入凭据,然后单击创建新的客户端ID"
    • 选定的"Web应用程序"
    • 将授权的javascript来源设置为 http://localhost:4567
    • 删除重定向URI中的所有内容并将其留空,然后按创建客户端ID".
  1. Created a project via the Google Developers Console.
  2. Opened up that project there, navigated to APIs under APIs & auth, and ensured that Drive API was enabled
  3. Went into credentials, and clicked on "Create a new Client ID"
    • Selected "Web Application"
    • Set authorized javascript origins to http://localhost:4567
    • Deleted any contents in Redirect URI's and left it blank, then pressed "Create Client ID."
  • 这不能仅仅通过在浏览器中打开来完成,所以我不得不托管一个本地服务器
  • 我在命令行中导航到该目录,然后键入"python -m SimpleHTTPServer 4567"(不带引号),并且该主机托管了本地服务器
  • 在我的网络浏览器中打开了 http://localhost:4567 ,所有这些示例在复制后都可以正常工作将新创建的客户ID放入这些文件中,以供他们索取.
  • This wouldn't run by simply opening in a brower, so I had to host a local server
  • I navigated into that directory in the command line and then typed "python -m SimpleHTTPServer 4567" (without the quotes) and this hosted a local server
  • Opened http://localhost:4567 in my web brower, and all of these samples work fine, after copying the newly created client ID into these files where they ask for it.

我还制作了一个python应用程序,以使用 pydrive I:

I also have made a python application, to use pydrive I:

  1. 单击创建新的客户端ID",然后单击已安装的应用程序"和其他",然后单击创建客户端ID".
  2. 接下来,我转到旧版Google API控制台,单击API Access,发现客户ID,然后单击下载JSON".
  3. 我将这个client_secrets.json放在我的python应用程序旁边,这使pydrive成功进行了身份验证,我可以使用该客户端ID在任何地方访问和修改我的Google驱动器文件.虽然我当然在将应用程序提供给其他人之前删除了此client_secrets.json,并向他们展示了如何执行此过程.
  1. Clicked on "Create a new Client ID", then "Installed Application" and "Other", then "Create Client ID."
  2. Next I went to the Old Google APIs Console, clicked on API Access, found that client ID, and clicked Download JSON.
  3. I placed this client_secrets.json next to my python application, and this allowed pydrive to authenticate successfully and I could access and modify my google drive files anywhere using that client ID. Though of course I deleted this client_secrets.json before giving the application to another person, and showed them how to do this process as well.

但是,除此之外,我不确定:

However, beyond this, I'm a little unsure about, specifically:

  • 一个人如何可以在Web应用程序中使用驱动器api而无需设置本地服务器,只需在jsfiddle中运行代码并使用客户端ID通过我的项目发送请求即可,
  • 如果设置的本地服务器可供网络上的任何人访问,那么如何修改上述步骤以允许任何客户端打开该服务器的网页以使用google drive API?

我知道我很可能需要在开发人员控制台中设置公共API"访问权限,但不确定是否也应使用​​哪些引荐来源网址.那么有没有一种简单的方法可以做到这一点?

I know that I most likely need to set up a Public API access in the developers console, but am not entirely sure what Referers I should use as well. So is there a simple way to do any of this?

我也知道 gspread 只能使用客户端的用户名和密码来打开google excell电子表格,因此我怀疑我在寻找什么是可能的,但是我不确定.

I also know that gspread can open google excell spreadsheets only using the client's username and password, so I'm suspecting that what I'm looking for is possible, but I'm not sure.

推荐答案

好的,所以我找到了一个效果很好的解决方案:

Okay, so I found a solution that works pretty well:

  1. 注册一个 OAuth.io 帐户(免费).
  2. 登录后,转到oauth.io 仪表板.
  3. 您应该查看的是默认应用,否则您可以制作另一个无关紧要的应用.
  4. 在域和URL白名单"下,您会看到一个小框,上面写着本地主机".输入 " http://fiddle.jshell.net/(不带引号),然后按Enter.这允许任何jsfiddle验证您的应用程序
  5. 接下来,我们需要启用Google云端硬盘支持
  1. Make an OAuth.io account (It's free).
  2. Once you're logged in, go to your oauth.io dashboard.
  3. You should be looking at a Default App, or you can make another one it doesn't really matter.
  4. Under "Domains & URLs whitelist", you'll see a little box that says "localhost." Type in " http://fiddle.jshell.net/" (without the quotes) and press enter. This allows any jsfiddle to authenticate with your application
  5. Next, we need to enable Google Drive support
    • Go to the Google Developers Console
    • Like before, navigate to Credentials under APIs and Auth.
    • Click "Create new Client ID"
    • Select Web Application
    • Under "Redirect URIs" put "https://oauth.io/auth" and "http://oauth.io/auth" (without quotes, on separate lines)
    • Under "Javascript Origins" put "https://oauth.io" and "http://oauth.io"
    • Press "Create Client ID"
  1. 选择在线(至少在线是首选,因为离线应仅用于服务器端)
  2. 点击范围"教科书,然后选择" https://www.googleapis.com/auth/驱动器(查看和管理您的Google云端硬盘中的文件)."
  3. 按完成.现在,单击尝试身份验证"按钮,希望您会得到一些输出,例如
  1. Select online (at least online is preferred since offline should be for server side only)
  2. Click on the Scope text book, and select "https://www.googleapis.com/auth/drive (View and manage the files in your google Drive)."
  3. Press finish. Now click on the Try Auth button, and hopefully you should get some output like


{
  "access_token": "ya29.AwH-1N_gnstLBuZfOR4W9CCcggKrQpMyKYV4QVEtCiIzHozNU5AfUJoYQzukALfjdiw2iOCUve7JbQ",
  "token_type": "Bearer",
  "expires_in": 3600,
  "provider": "google_drive"
}

要使用此功能,您可以执行以下操作:

To use this, you can do something like:

// This only works because we're set to "No wrap - in <head>"
function ShowDriveFileList() {
    var accessToken;
    // Initialize OAuth with our key
    OAuth.initialize('lmGlb8BaftfF4Y5en_c8XYOSq2A');

   // Connect to google drive, and print out file list
    OAuth.popup('google_drive').done(function (result) {
    var xmlHttp = null;
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", "https://www.googleapis.com/drive/v2/files", false);
        xmlHttp.setRequestHeader("Authorization", "Bearer " + result.access_token);
        xmlHttp.send(null);
        alert(xmlHttp.responseText);
    }).fail(function (err) {
        alert(err);
    });;
}

您可以在 http://jsfiddle.net/JMTVz/41/中找到.这使用了我的oauth.io客户端ID,但是您可以将其替换为您的ID,并且应该也可以正常工作.

Which you can find at http://jsfiddle.net/JMTVz/41/. This uses my oauth.io client id, but you can replace it with yours and it should work as well.

这篇关于如何设置Google云端硬盘客户端ID以便在没有本地服务器的情况下进行访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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