简体   繁体   English

Javascript:如何通过网络应用程序使用Google API访问“特定用户”数据?

[英]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.

相关问题 如何在我的Web Form应用程序中的任何地方访问来自Azure的用户数据? - How to access user data coming from Azure from everywhere in my Web Form App? (仍未解决)授权Web客户端访问客户端和服务器端的用户Google日历? (使用Firebase和Google API) - (still unresolved) Authorizing Web App access to user Google Calendar's on Client and Server side? (with Firebase and Google API) 如何使用来自 Javascript 的服务帐户在未经用户同意的情况下获取 Google API 数据的访问令牌 - How to get access token for Google APIs Data without User Consent using Service Account from Javascript 如何将数据从 Puppeteer 发布到 Google Apps 脚本 web 应用 API 端点 node.js 中? - How to POST data from Puppeteer to Google Apps Script web app API endpoint in node.js? 如何使用google-api-javascript-client和Javascript访问Google Photos API并读取JSON数据? - How to Access Google Photos API with Javascript using google-api-javascript-client and read JSON data? 如何从 Firebase (Web) 数据库中获取用户特定数据? - How to get a user specific data from Firebase (Web) Database? 如何用Javascript模仿Google API中的管理员用户? - How can I impersonate an admin user from Google API in Javascript? 使用Web开发人员提供的Web API从Android应用程序访问/更新服务器中的数据? - Access/Update data in server from android app using Web API provided by web developer? 如何使用JavaScript和HTML从REST API中选择特定数据 - How to select specific data from a REST API using JavaScript and HTML Javascript:如何从API提取特定数据? - Javascript: How do I fetch specific data from an API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM