POST 在 Javascript/Jquery 中请求 Google Admin SDK
POST request for Google Admin SDK in Javascript/Jquery
正在尝试使用 post 对目录 (Admin SDK) 的请求向用户添加别名。我知道 JS 客户端库仍处于测试阶段,但这是我要做的最简单的解决方案。
下面的 returns 一个 401 错误。我的凭据在控制台上没问题。
<script>
var apiKey = "my_key";
var clientId = "my_client_id";
var scopes = "https://www.googleapis.com/auth/admin.directory.user.alias";
var users = {}; // external JSON file
function load() {
gapi.client.setApiKey(apiKey);
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: true
},
function(){
for (var i = 0; i < users.length; i++) {
var thisUser = users[i]["emailAddress"];
var thisAlias = users[i]["secondaryEmail"];
var thisURL = "https://www.googleapis.com/admin/directory/v1/users/" + thisUser + "/aliases"
$.post(thisURL, {alias: thisAlias});
}
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=load"></script>
您可以尝试按照 JavaScript Quickstart 中的示例代码进行操作:
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = '<YOUR_CLIENT_ID>';
var SCOPES = ['https://www.googleapis.com/auth/admin.directory.user.readonly'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadDirectoryApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
尝试将您的代码格式更改为此,我认为这会起作用。
正在尝试使用 post 对目录 (Admin SDK) 的请求向用户添加别名。我知道 JS 客户端库仍处于测试阶段,但这是我要做的最简单的解决方案。
下面的 returns 一个 401 错误。我的凭据在控制台上没问题。
<script>
var apiKey = "my_key";
var clientId = "my_client_id";
var scopes = "https://www.googleapis.com/auth/admin.directory.user.alias";
var users = {}; // external JSON file
function load() {
gapi.client.setApiKey(apiKey);
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: true
},
function(){
for (var i = 0; i < users.length; i++) {
var thisUser = users[i]["emailAddress"];
var thisAlias = users[i]["secondaryEmail"];
var thisURL = "https://www.googleapis.com/admin/directory/v1/users/" + thisUser + "/aliases"
$.post(thisURL, {alias: thisAlias});
}
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=load"></script>
您可以尝试按照 JavaScript Quickstart 中的示例代码进行操作:
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = '<YOUR_CLIENT_ID>';
var SCOPES = ['https://www.googleapis.com/auth/admin.directory.user.readonly'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadDirectoryApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
尝试将您的代码格式更改为此,我认为这会起作用。