【问题标题】:Access GAPI authorization in PHP?在 PHP 中访问 GAPI 授权?
【发布时间】:2018-09-11 04:25:27
【问题描述】:

使用以下 JavaScript 代码,我授权自己访问 Google API。我使用它来使用 JavaScript 来设置 span 元素的内容。但是,我还需要使用 PHP 将这些数据保存到 MySql 数据库中。如何在 PHP 代码中从 JavaScript 访问授权?下面是 JavaScript 代码:

// Client ID and API key from the Developer Console
var CLIENT_ID = "XXX.apps.googleusercontent.com";
var API_KEY = "XXX";

// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"];

// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = "https://mail.google.com/ https://www.googleapis.com/auth/userinfo.email";

/**
 *  On load, called to load the auth2 library and API client library.
 */
function handleClientLoad() {
  gapi.load("client:auth2", initClient);
}

/**
 *  Initializes the API client library and sets up sign-in state
 *  listeners.
 */
function initClient() {
  gapi.client.init({
    apiKey: API_KEY,
    clientId: CLIENT_ID,
    discoveryDocs: DISCOVERY_DOCS,
    scope: SCOPES
  }).then(function () {
    document.getElementById("email").innerHTML = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile().getEmail();
  });
}

【问题讨论】:

    标签: javascript php oauth-2.0 google-api


    【解决方案1】:

    您可以通过 php 授权用户。另外,如果确实需要,可以通过AJAX请求发送数据。

    【讨论】:

    • 问题是,我不想授权用户两次,我需要在同一页面上同时使用 JavaScript 和 PHP 的授权。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2018-12-20
    • 2021-09-19
    相关资源
    最近更新 更多