[英]Javascript: how to access *specific user* data with Google API from web app?
I have a 'philosophical' doubt about Google API access from the browser (in my case it's Google Calendar API, but that does not matter). 我对从浏览器访问Google API有“哲学上的”疑问(在我的情况下是Google Calendar API,但这无关紧要)。
I am writing a nice (angular.js) web app for a small hotel; 我正在为一家小旅馆编写一个不错的(angular.js)网络应用程序; it sports a reservation form.
它预订表格。
I would like to link the reservation form to the hotel's Google Calendar account, to be able to show already reserved days as unavailable, and where to write eventual bookings. 我想将预订表格链接到酒店的Google日历帐户,以便将已经预订的日期显示为不可用,并在哪里写出最终的预订。
My doubt is this: the access to Google API from the browser requires oAuth2. 我的疑问是:从浏览器访问Google API需要oAuth2。 Fine.
精细。 But I don't want the web app user to give me access to his own account, but I want to be able to access the hotel account, whose clientId I know...
但是我不希望网络应用程序用户向我授予其自己的帐户的访问权限,但我希望能够访问酒店帐户,该帐户我知道其clientId ...
I'm sure I'm very confused, please give me some advise... :-( 我确定我很困惑,请给我一些建议... :-(
I have an app which uses a similar scenario but using Google Analytics, so what I have done is set up a Google service account and then downloaded and used the php auth library to set up an endpoint which handles the auth and gives me the data I request from the app. 我有一个使用类似场景但使用Google Analytics(分析)的应用程序,因此我要做的是设置一个Google服务帐户,然后下载并使用php auth库来设置一个端点来处理auth并为我提供数据从应用程序请求。
The relevant code is something like this: 相关代码如下:
google-data.php google-data.php
<?php
require_once '/path-to/google/src/Google/autoload.php';
$veiwId = $_GET['viewid']; // Sent from the app
$client_email = 'YOUR_SERVICE_ACCOUNT_EMAIL_HERE'; //looks something like - someproject@api-project-69734519371.iam.gserviceaccount.com
$private_key = file_get_contents('/path-to/APIProject-c5bfsdf45d54d.p12'); // downloaded from Google dev console
// Define the service you want to use (Calendar, Analytics, etc.)
$scopes = array(Google_Service_Analytics::ANALYTICS_READONLY);
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
// Grab token if it's set
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
// Refresh if expired
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
// Pin to Session
$_SESSION['service_token'] = $client->getAccessToken();
// Here is where you will start using the service, such as Google Calendar, etc.
$service = new Google_Service_Analytics($client);
// Adding Dimensions
$params = array('dimensions' => 'ga:medium');
// requesting the data (methods will depend on the service)
$data = $service->data_ga->get(
// params from get request
// request logic, etc.
);
// Return to client as JSON
echo json_encode($data);
?>
You obviously don't need to use the PHP library as there are plenty others available , however the concept remains the same. 您显然不需要使用PHP库,因为还有很多可用的库,但是概念仍然相同。 My use case was quite simple so PHP was the go-to.
我的用例非常简单,因此PHP是首选。
With that set up I was able to send a simple get request from my angular app for the data: 通过该设置,我能够从我的角度应用程序发送一个简单的get请求以获取数据:
app.factory('DataLoader', function( $http, $log ) {
return {
getGoogleData: function(toDate, fromDate, viewId) {
// Pass in some dates and a profile id
return $http.get('path-to/google-data.php?todate='+toDate+'&fromdate='+fromDate+'&viewid='+viewId);
}
}
})
Hopefully this may help you understand how you can allow your app to interact with the Google api intead of your end users interacting with the api. 希望这可以帮助您了解如何允许您的应用与与api进行交互的最终用户的Google api接口进行交互。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.