简体   繁体   English

如何设置Google云端硬盘客户端ID以在没有本地服务器的情况下进行访问?

[英]How to set up a Google Drive Client ID for access without a local server?

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? 我想知道是否有可能在随机计算机上打开jsfiddle并登录并进行身份验证并使用驱动器API,而不必一直运行本地服务器? 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. 如果这是一个简单的问题,很抱歉,但是我有点迷失了,因为到目前为止我发现的说明尚不清楚。

Edit: 编辑:

So far I have, following from here and here : 到目前为止,我已经从这里这里跟随:

  1. Created a project via the Google Developers Console . 通过Google Developers Console创建了一个项目。
  2. Opened up that project there, navigated to APIs under APIs & auth, and ensured that Drive API was enabled 在此处打开该项目,导航至API和auth下的API,并确保已启用Drive API
  3. Went into credentials, and clicked on "Create a new Client ID" 进入凭据,然后单击“创建新的客户端ID”
    • Selected "Web Application" 选择“ Web应用程序”
    • Set authorized javascript origins to http://localhost:4567 将授权的javascript来源设置为http:// localhost:4567
    • Deleted any contents in Redirect URI's and left it blank, then pressed "Create Client ID." 删除重定向URI中的所有内容,并将其留空,然后按“创建客户端ID”。
  4. Took a sample like this , this , or this , and stored it in a local file named named index.html. 提取了一个类似thisthisthis的示例,并将其存储在名为index.html的本地文件中。
    • 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 我在命令行中导航到该目录,然后键入“ python -m SimpleHTTPServer 4567”(不带引号),并且此主机托管了本地服务器
    • 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. 在我的网络浏览器中打开http:// localhost:4567 ,将新创建的客户端ID复制到这些文件中它们要求的位置之后,所有这些示例都可以正常工作。

I also have made a python application, to use pydrive I: 我还制作了一个python应用程序,以使用pydrive I:

  1. Clicked on "Create a new Client ID", then "Installed Application" and "Other", then "Create Client ID." 单击“创建新的客户端ID”,然后单击“已安装的应用程序”和“其他”,然后单击“创建客户端ID”。
  2. Next I went to the Old Google APIs Console , clicked on API Access, found that client ID, and clicked Download JSON. 接下来,我转到旧的Google API控制台 ,单击API Access,找到该客户端ID,然后单击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. 我将这个client_secrets.json放在我的python应用程序旁边,这使pydrive能够成功进行身份验证,并且可以使用该客户端ID在任何地方访问和修改我的Google驱动器文件。 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. 尽管我当然在将应用程序提供给其他人之前删除了此client_secrets.json,并向他们展示了如何执行此过程。

However, beyond this, I'm a little unsure about, specifically: 但是,除此之外,我不确定:

  • How one can use the drive api in web applications without having to set up a local server, say simply by running code in jsfiddle and having requests sent through my project via using a Client ID, and 无需设置本地服务器,如何在Web应用程序中使用驱动器api,只需在jsfiddle中运行代码并使用客户端ID通过我的项目发送请求,以及
  • If a local server is set up that can be accessed by anyone on the web, how one can modify the above steps to allow any client to open that server's webpage to use the google drive API? 如果设置了可以由网络上的任何人访问的本地服务器,那么如何修改上述步骤以允许任何客户端打开该服务器的网页以使用google drive 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. 我知道我很可能需要在开发人员控制台中设置“公共API”访问权限,但不确定是否也应使用​​哪些引荐来源网址。 So is there a simple way to do any of this? 那么有没有简单的方法可以做到这一点?

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

Okay, so I found a solution that works pretty well: 好的,所以我找到了一个很好用的解决方案:

  1. Make an OAuth.io account (It's free). 注册一个OAuth.io帐户(免费)。
  2. Once you're logged in, go to your oauth.io dashboard . 登录后,转到oauth.io 仪表板
  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." 在“域和URL白名单”下,您会看到一个小框,上面写着“本地主机”。 Type in " http://fiddle.jshell.net/ " (without the quotes) and press enter. 输入“ http://fiddle.jshell.net/ ”(不带引号),然后按Enter键。 This allows any jsfiddle to authenticate with your application 这允许任何jsfiddle使用您的应用程序进行身份验证
  5. Next, we need to enable Google Drive support 接下来,我们需要启用Google云端硬盘支持
    • Go to the Google Developers Console 转到Google Developers Console
    • Like before, navigate to Credentials under APIs and Auth. 像以前一样,导航到API和Auth下的Credentials。
    • Click "Create new Client ID" 点击“创建新的客户ID”
    • Select Web Application 选择网络应用
    • Under "Redirect URIs" put " https://oauth.io/auth " and " http://oauth.io/auth " (without quotes, on separate lines) 在“重定向URI”下,放置“ https://oauth.io/auth ”和“ http://oauth.io/auth ”(不带引号,在单独的行上)
    • Under "Javascript Origins" put " https://oauth.io " and " http://oauth.io " 在“ Javascript起源”下,放置“ https://oauth.io ”和“ http://oauth.io
    • Press "Create Client ID" 按“创建客户端ID”
  6. Now go to your Oauth.io key manager . 现在转到您的Oauth.io密钥管理器 Click on Google Drive in the list to the left. 点击左侧列表中的Google云端硬盘。 Press next, and enter your client_id and client_secret that you created in the previous step (via the Google Developers Console) 按下一步,然后输入您在上一步中创建的client_id和client_secret(通过Google Developers Console)
    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)." 单击“作用域”教科书,然后选择“ https://www.googleapis.com/auth/drive (查看和管理Google云端硬盘中的文件)。”
    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);
    });;
}

Which you can find at http://jsfiddle.net/JMTVz/41/ . 您可以在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. 这使用了我的oauth.io客户端ID,但是您可以将其替换为您的ID,并且它也应该可以正常工作。

You can do the same thing using the OAuth Playground. 您可以使用OAuth Playground做同样的事情。

See How do I authorise an app (web or installed) without user intervention? 请参阅如何在没有用户干预的情况下授权应用程序(网络或已安装)? (canonical ?) (规范?)

Step 11 will be different. 步骤11将有所不同。 Instead of pasting the refresh token into your server app (which you don't have), you'll paste the access token into your JS in the same way as you are doing in your answer. 无需将刷新令牌粘贴到您的服务器应用程序中(您没有),而是将访问令牌粘贴到您的JS中,就像在回答中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM