久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Google Drive API + Parse 將文件存儲在單個 Driv

Using Google Drive API + Parse to store files on a single Drive account(使用 Google Drive API + Parse 將文件存儲在單個 Drive 帳戶上)
本文介紹了使用 Google Drive API + Parse 將文件存儲在單個 Drive 帳戶上的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試將單個 Google Drive 帳戶用作網(wǎng)絡(luò)服務(wù)器".我有一個需要能夠存儲和檢索圖片的 Android 應(yīng)用程序.我的想法是使用 Parse 來幫助管理所有內(nèi)容并將我的存儲容量擴(kuò)展到 Parse 的可用容量之外.

I am trying to use a single Google Drive account as a 'web server.' I have an Android app that needs to be able to store and retrieve pictures. My idea was to use Parse to help manage everything and extend my storage capacity beyond Parse's free amount.

基本上,我將擁有一個 Google Drive 帳戶和一個 Parse 項目.當(dāng)用戶想要存儲文件時,他/她將文件上傳到 Parse,Parse 使用單個 Google Drive 帳戶(使用 CloudCode)進(jìn)行身份驗證,Parse 將文件上傳到 Drive,將文件的 URL 存儲在表中,然后刪除Parse 的云存儲中的文件.我計劃為存儲這些文件的文件夾提供私有寫入訪問權(quán)限和公共讀取訪問權(quán)限,這樣客戶端就不必發(fā)出 Parse 請求

Essentially, I will have a single Google Drive account and a Parse project. When a user wants to store a file, he/she uploads the file to Parse, Parse authenticates with the single Google Drive account (using CloudCode), Parse uploads the files to Drive, stores the URL to the file in a table, and deletes the file from Parse's cloud storage. I plan on giving the folder that stores these files private write access and public read access so the clients don't have to make Parse requests to

這樣做的目的是為我的應(yīng)用程序獲得更多存儲空間.(Amazon S3 只提供 5g,Parse 提供 1g,DropBox 2g,Google Cloud Storage 我不認(rèn)為他們有免費計劃,Drive 提供 15g 但我也聽說過 Google Photos 與 Google Drive 集成,這可能會給我無限的存儲空間圖片)

The purpose of this is to get more storage for my application. (Amazon S3 only gives 5g, Parse gives 1g, DropBox 2g, Google Cloud Storage I don't think they have a free plan, and Drive gives 15g but I have also heard about Google Photos integrating with Google Drive which might give me unlimited storage for pictures)

由于 Google 云端硬盤并非真正設(shè)計用于執(zhí)行此操作,因此我很難弄清楚所有部分是如何組合在一起的.

Because Google Drive wasn't really designed to do this, I am having some difficultly figuring out how all the pieces fit together.

我看過這個 問題 似乎不適用于我的情況,因為我將能夠在安全服務(wù)器上運行所有寫操作.(我也讀過我需要存儲一個刷新令牌,使用這種方法也應(yīng)該是安全的)

I have looked at this question which doesn't seem to apply to my situation because I will be able to run all my write operations on a secure sever. (Also I have read I need to store a refresh token which should also be secure using this method)

我查看了 this 但許多鏈接都已過時,所以沒有對我?guī)椭淮?

I have looked at this but many of the links are outdated, so that didn't help me much.

我查看了 this,但這似乎是在授權(quán)應(yīng)用程序的使用用戶的個人文件.

I have looked at this, but this seems to be authorizing an app's use of the user's personal files.

我還讀到如果使用 Web 應(yīng)用程序帳戶,我可能需要使用服務(wù)帳戶,但同樣,我一直在閱讀的信息不是很清楚.在我看來,這是因為云端硬盤的設(shè)計初衷并非如此.

I have also read that I may need to use a service account instead if a Web Application account, but again, the information I have been reading has not been very clear. In my opinion, this is because Drive was not designed to work this way.

總結(jié):

誰能指出我使用 Parse Cloud Code(服務(wù)器端 Javascript)將文件寫入單個 Google Drive 帳戶的正確方向?

如果上述問題可以解決/可能,Google 發(fā)布 Google Photo 是否意味著我將擁有基本上無限的圖片存儲容量?

推薦答案

好的.因此,經(jīng)過數(shù)小時的研究和測試,我想出了一種讓它發(fā)揮作用的方法.

OK. So after hours of research and testing, I have come up with a way of getting this to work.

Google 的 API 身份驗證過程至少可以說有點令人困惑.一般流程如下:

Google's API authentication process is a bit confusing to say the least. The general procedure is as follows:

  1. 要求用戶授予對 INSERT YOUR PROJECT HERE 范圍 YOUR SCOPE
  2. 的訪問權(quán)限
  3. 用戶可以接受或拒絕該請求
  4. 如果用戶接受,您可以檢索訪問令牌(更重要的是刷新令牌)
  5. 獲得刷新令牌后,您可以獲得新的訪問令牌,以便隨時進(jìn)行 API 調(diào)用.

所以整個目標(biāo)是進(jìn)行需要刷新代碼的 API 調(diào)用(特別是驅(qū)動).

So the whole goal of this is to make API calls (specifically to drive) which requires a refresh code.

這方面的主要困難是(據(jù)我所知)無法在 Parse CloudCode 中驗證用戶并獲取訪問代碼.但是,我有一個理論,如果我能夠以某種方式在 CloudCode 之外驗證用戶帳戶,您仍然可以制作 GET/POST 來自 CloudCode 函數(shù)內(nèi)部的請求.(更具體地說:Parse.Cloud.httpRequest)

The main difficulty in this is, (AS FAR AS I KNOW) there is no way of authenticating a user and getting an access code inside of Parse CloudCode. BUT, I had a theory that if I was somehow able to authenticate the user account outside of CloudCode, you could still make GET/POST requests from inside a CloudCode function. (More specifically: Parse.Cloud.httpRequest)

有許多方法/平臺可讓您對用戶進(jìn)行身份驗證以獲取刷新代碼.最容易使用的方法可能是使用 API 的 Android/Java 版本,因為任何有計算機(jī)的人都可以運行 Android 模擬器,但我可以輕松訪問支持 PHP 的網(wǎng)站,所以我選擇使用 PHP.但同樣,這部分過程可以在許多不同的平臺上完成.

There are many ways/platforms that allow you to authenticate a user in order to get a refresh code. The most accessible method would probably be by using the Android/Java version of the API because anyone with a computer can run an Android emulator, but I have easy access to a PHP capable website, so I chose to use PHP instead. But again, this part of the process can be done on many different platforms.

第一步是安裝 Google API Client Library for PHP (Github).有幾種不同的方法可以在您的服務(wù)器上安裝它,但由于我只需要它來獲取刷新令牌,因此我選擇在運行時動態(tài)包含它(也是因為我無法快速訪問我的 php.ini 文件).ini 文件).

The first step is to install the Google API Client Library for PHP (Github). There are a few different ways to install this on your server, but since I only need it to get a refresh token, I chose to include it dynamically at runtime (also because I didn't have quick access to my php.ini file).

(在我繼續(xù)之前請注意:我不是 PHP 開發(fā)人員,所以如果我做了任何奇怪或多余的事情,請告訴我;我只是展示了我為使其工作所做的工作)

(Note before I continue: I am not a PHP developer, so please let me know if I do anything odd or redundant; I am merely showing what I did to get this to work)

為此,我只需下載該庫的副本,將其上傳到我的服務(wù)器,并在我使用該庫的所有文件中包含以下行:

To do this I simply downloaded a copy of the library, uploaded it to my server, and included the following line in all the files I used the library:

set_include_path(get_include_path() . PATH_SEPARATOR . 'google-api-php-client-master/src');

其中 google-api-php-client-master/src 是您的 src 文件夾的路徑.

Where google-api-php-client-master/src is the path to your src folder.

完成后,您必須對用戶進(jìn)行身份驗證.以下兩個文件將用戶發(fā)送到身份驗證頁面,然后在屏幕上打印該用戶的刷新代碼.

Once you have that done, you have to authenticate the user. The following two files send the user to an authentication page, and then print the refresh code for that user on the screen.

index.php:

<?php
    set_include_path(get_include_path() . PATH_SEPARATOR . 'google-api-php-client-master/src');
    require_once 'google-api-php-client-master/src/Google/autoload.php'; // or wherever autoload.php is located 

    session_start();

    $client = new Google_Client();
    $client->setAuthConfigFile('client_secrets.json'); //You can download this file from your developer console under OAuth 2.0 client IDs
    $client->addScope(Google_Service_Drive::DRIVE); //Scope that grants full access to user's Google Drive
    $client->setAccessType("offline"); //Important so a refresh code is returned

    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        print($_SESSION['refresh_token']);
    } else {
        $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
?>

oauth2callback.php:

<?php

    set_include_path(get_include_path() . PATH_SEPARATOR . 'google-api-php-client-master/src');

    require_once 'google-api-php-client-master/src/Google/autoload.php';

    session_start();

    $client = new Google_Client();
    $client->setAuthConfigFile('client_secrets.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
    $client->addScope(Google_Service_Drive::DRIVE);
    $client->setAccessType("offline");

    if (! isset($_GET['code'])) {
      $auth_url = $client->createAuthUrl();
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    } else {
      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();
      $_SESSION['refresh_token'] = $client->getRefreshToken(); //Important to clear the session variable after you have the token saved somewhere
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/'; //Redirects to index.php
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }

?>

關(guān)于此代碼的幾個重要說明:

Several important notes about this code:

  1. 您需要確保您已創(chuàng)建適當(dāng)?shù)膽{據(jù)(網(wǎng)絡(luò)應(yīng)用程序)
  2. 您需要將正確的重定向 URI 添加到您的 OAuth 2.0 客戶端 ID.它應(yīng)該類似于 http://yourdomain.com/oauth2callback.php.
  3. 您需要將 client_secrets.json 文件上傳到您的服務(wù)器.您可以通過轉(zhuǎn)到開發(fā)人員控制臺并單擊 OAuth 2.0 客戶端 ID 列表最右側(cè)的下載圖標(biāo)來獲取此信息.
  4. 出于安全原因,您可能應(yīng)該在復(fù)制刷新令牌后清除會話變量.
  1. You need to make sure you have created the appropriate credentials (Web application)
  2. You need to add the correct redirect URI to your OAuth 2.0 client ID. It should be something like http://yourdomain.com/oauth2callback.php.
  3. You need to have uploaded your client_secrets.json file to your server. You can get this by going to your developers console and clicking the download icon on the far right side of the OAuth 2.0 client IDs list.
  4. You should probably clear your session variables after you copy the refresh token down for security reasons.

因此,總而言之,我們創(chuàng)建了一個簡單的程序,可以為特定用戶打印出刷新令牌.您需要將該代碼復(fù)制下來并保存以備后用.

So in summary, we have created a simple program that prints out the refresh token for a specific user. You need to copy that code down and save it for later.

即使您擁有刷新令牌,您也需要一種獲取訪問令牌以進(jìn)行 API 調(diào)用的方法.(如果沒有訪問令牌,您無法進(jìn)行 API 調(diào)用,而且由于它們每 3600 秒過期一次,因此您需要一種在需要時獲取新令牌的方法)

Even though you have the refresh token, you need a way of getting an access token to make API calls. (You can't make an API call without an access token, and because they expire every 3600 seconds, you need a way of getting a new one when you need it)

這是完成此步驟的 Parse CloudCode:

Here is the Parse CloudCode for accomplishing this step:

Parse.Cloud.define("getAccessToken", function(request, response) {
    Parse.Cloud.httpRequest({
        method: "POST",
        url: 'https://www.googleapis.com/oauth2/v3/token/',
        params: {
            refresh_token : 'INSERT_REFRESH_TOKEN_HERE',
            client_id : 'INSERT_CLIENT_ID_HERE',
            client_secret : 'INSERT_CLIENT_SECRET_HERE',
            grant_type : 'refresh_token'
        }
    }).then(function(httpResponse) {
      response.success(httpResponse.text);
    }, function(httpResponse) {
      response.error('Request failed with response code ' + httpResponse.status);
    });
});

這個函數(shù)發(fā)送一個訪問令牌的請求.你可以用那個令牌做任何你想做的事;它存儲在 httpResponse.text 中(如果請求成功).請注意,grant_type 應(yīng)該是 refresh_token 而不是您的刷新令牌.

This function sends a request for an access token. You can do whatever you want with that token; it is stored in httpResponse.text (if the request was successful). Note that grant_type should be refresh_token not YOUR refresh token.

獲得訪問令牌后,您就可以在刷新令牌范圍內(nèi)自由地進(jìn)行 API 調(diào)用.示例:

Once you have an access token, you are free to make API calls within the scope of your refresh token. Example:

Parse.Cloud.define("sendRequest", function(request, response) {
    Parse.Cloud.httpRequest({
        method: "GET",
        url: 'https://www.googleapis.com/drive/v2/about',
        params: {
            access_token : 'INSERT_YOUR_ACCESS_TOKEN_HERE'
        }
    }).then(function(httpResponse) {
      response.success(httpResponse.text);
    }, function(httpResponse) {
      response.error('Request failed with response code ' + httpResponse.status);
    });
});

此函數(shù)返回有關(guān)與該刷新令牌關(guān)聯(lián)的驅(qū)動器帳戶的信息.

This function returns information about the drive account associated with that refresh token.

關(guān)于我的第二個問題,我還沒有做足夠的研究來知道答案,但我計劃找出并在這里發(fā)布.

With regards to my second question, I have not done enough research to know the answer to that yet, but I plan on finding out and posting it here.

這篇關(guān)于使用 Google Drive API + Parse 將文件存儲在單個 Drive 帳戶上的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時的歡迎消息)
主站蜘蛛池模板: 香蕉超碰 | 一级做a | 人人干在线 | 特黄特色大片免费视频观看 | 亚洲第一成人影院 | 一区二区三区精品视频 | 国产一区影院 | 欧美激情一区 | 伦理午夜电影免费观看 | 黄久久久 | 国产91丝袜在线播放 | 免费一级片 | 国产精品一区在线观看 | 91久久精品一区二区二区 | 国产精品视频一 | 欧美一级黄带 | 免费高清av | 国产欧美日韩综合精品一区二区 | 国产午夜精品一区二区三区四区 | 亚洲欧美一区二区在线观看 | 久草综合在线视频 | 黑人巨大精品欧美一区二区免费 | 欧美视频二区 | 99亚洲精品| 日韩欧美在线视频 | 国产精品视频在 | 理论片午午伦夜理片影院 | 日日干夜夜操天天操 | 国产精品国产三级国产播12软件 | 亚洲国产成人精品久久久国产成人一区 | 毛片一级片| 国产福利久久 | 国产欧美精品在线 | 91麻豆精品国产91久久久久久 | 日韩国产在线观看 | 国产精品永久久久久久久www | 三级视频在线观看电影 | 中文字幕 在线观看 | 成人亚洲网站 | 成人精品在线观看 | 精品久久久久香蕉网 |