在PHP中访问GAPI授权?

时间:2018-04-01 22:04:18

标签: javascript php oauth-2.0 google-api

使用以下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();
  });
}

1 个答案:

答案 0 :(得分:0)

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