Thực thi Tập lệnh ứng dụng
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Thực thi chức năng
/**
* Executes an Apps Script function in a remote Apps Script.
* See: https://developers.google.com/google-ads/scripts/docs/features/third-party-apis#refresh_token_grant
* for details on configuring this script.
*
* NOTE: This script also requires the OAuth2 library to be pasted at the end,
* as obtained from https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library
*/
const CLIENT_ID = 'INSERT_CLIENT_ID';
const CLIENT_SECRET = 'INSERT_CLIENT_SECRET';
const REFRESH_TOKEN = 'INSERT_REFRESH_TOKEN';
// Enter scopes which should match scopes in File > Project properties
// For this project, e.g.: https://www.googleapis.com/auth/drive
const SCOPES = 'ENTER_SPACE_DELIMITED_SCOPES';
// Script ID taken from 'File > Project Properties'
const SCRIPT_ID = 'ENTER_SCRIPT_ID';
let authUrlFetch;
// Call this function just once, to initialize the OAuth client.
function initializeOAuthClient() {
if (typeof OAuth2 === 'undefined') {
const libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library';
throw Error(`OAuth2 library not found. Please take a copy of the OAuth2 ` +
`library from ${libUrl} and append to the bottom of this script.`);
}
const tokenUrl = 'https://accounts.google.com/o/oauth2/token';
authUrlFetch = OAuth2.withRefreshToken(tokenUrl, CLIENT_ID, CLIENT_SECRET,
REFRESH_TOKEN, SCOPES);
}
/**
* Execute a remote function.
* @param {string} remoteFunctionName The name of the function to execute.
* @param {Object[]} functionParams An array of JSON objects to pass to the
* remote function.
* @return {?Object} The return value from the function.
*/
function executeRemoteFunction(remoteFunctionName, functionParams) {
const apiParams = {
'function': remoteFunctionName,
'parameters': functionParams
};
const httpOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
payload: JSON.stringify(apiParams)
};
const url = `https://script.googleapis.com/v1/scripts/${SCRIPT_ID}:run`;
const response = authUrlFetch.fetch(url, httpOptions);
const data = JSON.parse(response.getContentText());
// Retrieve the value that has been returned from the execution.
if (data.error) {
throw Error(`There was an error: ${response.getContentText()}`);
}
return data.response.result;
}
// Paste in OAuth2 library here, from:
// https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-21 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-21 UTC."],[[["\u003cp\u003eThis script allows execution of a function within a remote Google Apps Script using OAuth2 for authorization.\u003c/p\u003e\n"],["\u003cp\u003eRequires setting up client ID, client secret, refresh token, scopes, and the remote script ID.\u003c/p\u003e\n"],["\u003cp\u003eUses \u003ccode\u003einitializeOAuthClient\u003c/code\u003e function for one-time OAuth client setup and \u003ccode\u003eexecuteRemoteFunction\u003c/code\u003e to trigger the remote function.\u003c/p\u003e\n"],["\u003cp\u003eThe script expects the OAuth2 library to be pasted within it for proper functionality.\u003c/p\u003e\n"],["\u003cp\u003eError handling is included to manage unsuccessful executions of the remote function.\u003c/p\u003e\n"]]],[],null,["# Apps Scripts execution\n\nExecute a function\n------------------\n\n```gdscript\n/**\n * Executes an Apps Script function in a remote Apps Script.\n * See: https://developers.google.com/google-ads/scripts/docs/features/third-party-apis#refresh_token_grant\n * for details on configuring this script.\n *\n * NOTE: This script also requires the OAuth2 library to be pasted at the end,\n * as obtained from https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library\n */\nconst CLIENT_ID = 'INSERT_CLIENT_ID';\nconst CLIENT_SECRET = 'INSERT_CLIENT_SECRET';\nconst REFRESH_TOKEN = 'INSERT_REFRESH_TOKEN';\n// Enter scopes which should match scopes in File \u003e Project properties\n// For this project, e.g.: https://www.googleapis.com/auth/drive\nconst SCOPES = 'ENTER_SPACE_DELIMITED_SCOPES';\n// Script ID taken from 'File \u003e Project Properties'\nconst SCRIPT_ID = 'ENTER_SCRIPT_ID';\n\nlet authUrlFetch;\n\n// Call this function just once, to initialize the OAuth client.\nfunction initializeOAuthClient() {\n if (typeof OAuth2 === 'undefined') {\n const libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library';\n throw Error(`OAuth2 library not found. Please take a copy of the OAuth2 ` +\n `library from ${libUrl} and append to the bottom of this script.`);\n }\n const tokenUrl = 'https://accounts.google.com/o/oauth2/token';\n authUrlFetch = OAuth2.withRefreshToken(tokenUrl, CLIENT_ID, CLIENT_SECRET,\n REFRESH_TOKEN, SCOPES);\n}\n\n/**\n * Execute a remote function.\n * @param {string} remoteFunctionName The name of the function to execute.\n * @param {Object[]} functionParams An array of JSON objects to pass to the\n * remote function.\n * @return {?Object} The return value from the function.\n */\nfunction executeRemoteFunction(remoteFunctionName, functionParams) {\n const apiParams = {\n 'function': remoteFunctionName,\n 'parameters': functionParams\n };\n const httpOptions = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json'\n },\n payload: JSON.stringify(apiParams)\n };\n const url = `https://script.googleapis.com/v1/scripts/${SCRIPT_ID}:run`;\n const response = authUrlFetch.fetch(url, httpOptions);\n const data = JSON.parse(response.getContentText());\n // Retrieve the value that has been returned from the execution.\n if (data.error) {\n throw Error(`There was an error: ${response.getContentText()}`);\n }\n return data.response.result;\n}\n\n// Paste in OAuth2 library here, from:\n// https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library\n```"]]