Twitter (OAuth1.0)
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
// User-level Twitter API request // Requires the OAuth1 library to be pasted into the script. // https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library const CONSUMER_KEY = 'INSERT_CONSUMER_KEY_HERE'; const CONSUMER_SECRET = 'INSERT_CONSUMER_SECRET_HERE'; const ACCESS_TOKEN = 'INSERT_ACCESS_TOKEN_HERE'; const ACCESS_SECRET = 'INSERT_ACCESS_SECRET_HERE'; /** * Retrieves the tweets for the specified Twitter user. * @param {string} screenName The Twitter name of the user, e.g. 'sundarpichai' * @return {?Object} The complex response object containing tweets, or null if * failure. See https://dev.twitter.com/rest/reference/get/statuses/user_timeline * for structure of this object. */ function getTweetsForUser(screenName) { if (typeof OAuth1 === 'undefined') { const libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library'; throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' + 'library from ' + libUrl + ' and append to the bottom of this script.'); } const params = {screen_name: screenName}; const authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET); const response = authUrlFetch .fetch('https://api.twitter.com/1.1/statuses/user_timeline.json', params); const responseText = response.getContentText(); return JSON.parse(responseText); } // Paste the OAuth1 library here.
// User-level Twitter API request // Requires the OAuth1 library to be pasted into the script. // https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library const CONSUMER_KEY = 'INSERT_CONSUMER_KEY_HERE'; const CONSUMER_SECRET = 'INSERT_CONSUMER_SECRET_HERE'; const ACCESS_TOKEN = 'INSERT_ACCESS_TOKEN_HERE'; const ACCESS_SECRET = 'INSERT_ACCESS_SECRET_HERE'; /** * Sends a tweet. * @param {string} message The message to send. * @return {?Object} The complex response object with the status of the send * request. See https://dev.twitter.com/rest/reference/post/statuses/update * for the structure of this object. */ function sendTweet(message) { if (typeof OAuth1 === 'undefined') { const libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library'; throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' + 'library from ' + libUrl + ' and append to the bottom of this script.'); } const params = ''; const tweet = message.substring(0, 160); const options = {method: 'POST', payload: {status: tweet}}; const authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET); const response = authUrlFetch .fetch('https://api.twitter.com/1.1/statuses/update.json', params, options); const responseText = response.getContentText(); return JSON.parse(responseText); }
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\u003cp\u003eThis script enables retrieval of tweets for a specified Twitter user using their screen name and the Twitter API.\u003c/p\u003e\n"],["\u003cp\u003eIt also allows sending tweets with a message of up to 160 characters through the Twitter API.\u003c/p\u003e\n"],["\u003cp\u003eBoth functionalities require the OAuth1 library to be included in the script for authentication and authorization with Twitter.\u003c/p\u003e\n"],["\u003cp\u003eBefore utilizing the script, ensure you replace placeholder values for consumer key, consumer secret, access token, and access secret with your actual Twitter API credentials.\u003c/p\u003e\n"]]],[],null,["Retrieve tweets for a specified user \n\n```carbon\n// User-level Twitter API request\n// Requires the OAuth1 library to be pasted into the script.\n// https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library\n\nconst CONSUMER_KEY = 'INSERT_CONSUMER_KEY_HERE';\nconst CONSUMER_SECRET = 'INSERT_CONSUMER_SECRET_HERE';\nconst ACCESS_TOKEN = 'INSERT_ACCESS_TOKEN_HERE';\nconst ACCESS_SECRET = 'INSERT_ACCESS_SECRET_HERE';\n\n/**\n * Retrieves the tweets for the specified Twitter user.\n * @param {string} screenName The Twitter name of the user, e.g. 'sundarpichai'\n * @return {?Object} The complex response object containing tweets, or null if\n * failure. See https://dev.twitter.com/rest/reference/get/statuses/user_timeline\n * for structure of this object.\n */\nfunction getTweetsForUser(screenName) {\n if (typeof OAuth1 === 'undefined') {\n const libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library';\n throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' +\n 'library from ' + libUrl + ' and append to the bottom of this script.');\n }\n const params = {screen_name: screenName};\n const authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET,\n ACCESS_TOKEN, ACCESS_SECRET);\n const response = authUrlFetch\n .fetch('https://api.twitter.com/1.1/statuses/user_timeline.json', params);\n const responseText = response.getContentText();\n return JSON.parse(responseText);\n}\n\n// Paste the OAuth1 library here.\n```\n\nSend a tweet \n\n```gdscript\n// User-level Twitter API request\n// Requires the OAuth1 library to be pasted into the script.\n// https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library\n\nconst CONSUMER_KEY = 'INSERT_CONSUMER_KEY_HERE';\nconst CONSUMER_SECRET = 'INSERT_CONSUMER_SECRET_HERE';\nconst ACCESS_TOKEN = 'INSERT_ACCESS_TOKEN_HERE';\nconst ACCESS_SECRET = 'INSERT_ACCESS_SECRET_HERE';\n\n/**\n * Sends a tweet.\n * @param {string} message The message to send.\n * @return {?Object} The complex response object with the status of the send\n * request. See https://dev.twitter.com/rest/reference/post/statuses/update\n * for the structure of this object.\n */\nfunction sendTweet(message) {\n if (typeof OAuth1 === 'undefined') {\n const libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library';\n throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' +\n 'library from ' + libUrl + ' and append to the bottom of this script.');\n }\n const params = '';\n const tweet = message.substring(0, 160);\n const options = {method: 'POST', payload: {status: tweet}};\n const authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET,\n ACCESS_TOKEN, ACCESS_SECRET);\n const response = authUrlFetch\n .fetch('https://api.twitter.com/1.1/statuses/update.json', params,\n options);\n const responseText = response.getContentText();\n return JSON.parse(responseText);\n}\n```"]]