【发布时间】: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