-3
我在APPS腳本中創建了一個腳本,並將其部署爲API可執行文件。連接時我開始出現500錯誤。我決定從教程中創建一個腳本來將這個錯誤本地化。令我驚訝的是,我收到了同樣的結果。可能這個問題與服務帳戶有關,因爲我將腳本與項目相關聯,我有服務帳戶,我嘗試使用它進行授權。無法訪問Google Execution API並收到500內部錯誤
我讀到使用服務帳戶連接到Execution API時存在問題,該問題今天仍然存在。
***更新:使用OAuth2用戶客戶端ID時,我已成功訪問帶有教程腳本的執行API。 使用服務帳戶連接到執行API仍然存在問題嗎?
這裏是Google Apps腳本,我部署爲可執行的API的項目,我有服務帳戶:
/**
* The function in this script will be called by the Apps Script Execution API. */
/**
* Return the set of folder names contained in the user's root folder as an
* object (with folder IDs as keys).
* @return {Object} A set of folder names keyed by folder ID.
*/
function getFoldersUnderRoot() {
var root = DriveApp.getRootFolder();
var folders = root.getFolders();
var folderSet = {};
while (folders.hasNext()) {
var folder = folders.next();
folderSet[folder.getId()] = folder.getName();
}
return folderSet;
}
這裏是我的部分修改後的代碼,我從教程了。我改變了它的服務帳戶的工作:
from googlepackage.api_client import *
from googlepackage.constants import *
from googleapiclient.errors import HttpError
from httplib2 import Http
def __get_credentials(scopes: list) -> ServiceAccountCredentials:
credential_dir = os.path.join("..\\", SERVICE_ACCOUNT_FOLDER)
print(credential_dir)
if not os.path.exists(credential_dir):
raise Exception("Cannot find the directory with credentials")
try:
credentials = ServiceAccountCredentials.from_json_keyfile_name(
os.path.join(credential_dir, 'file.json'),
scopes)
except (ValueError, KeyError) as error:
raise Exception(error)
return credentials
def main():
"""Shows basic usage of the Apps Script Execution API.
Creates a Apps Script Execution API service object and uses it to call an
Apps Script function to print out a list of folders in the user's root
directory.
"""
SCRIPT_ID = 'API ID'
http_auth = __get_credentials([READ_AND_WRITE_SCOPE, DRIVE_API_SCOPE]).authorize(Http())
# Authorize and create a service object.
service = discovery.build('script', 'v1', http=http_auth)
# Create an execution request object.
request = {"function": "getFoldersUnderRoot"}
try:
# Make the API request.
response = service.scripts().run(body=request, scriptId=SCRIPT_ID).execute()
if 'error' in response:
# The API executed, but the script returned an error.
# Extract the first (and only) set of error details. The values of
# this object are the script's 'errorMessage' and 'errorType', and
# an list of stack trace elements.
error = response['error']['details'][0]
print("Script error message: {0}".format(error['errorMessage']))
if 'scriptStackTraceElements' in error:
# There may not be a stacktrace if the script didn't start
# executing.
print("Script error stacktrace:")
for trace in error['scriptStackTraceElements']:
print("\t{0}: {1}".format(trace['function'],
trace['lineNumber']))
else:
# The structure of the result will depend upon what the Apps Script
# function returns. Here, the function returns an Apps Script Object
# with String keys and values, and so the result is treated as a
# Python dictionary (folderSet).
folderSet = response['response'].get('result', {})
if not folderSet:
print('No folders returned!')
else:
print('Folders under your root folder:')
for (folderId, folder) in folderSet.items():
print("\t{0} ({1})".format(folder, folderId))
except HttpError as e:
# The API encountered a problem before the script started executing.
print(e.content)
if __name__ == '__main__':
main()
這裏是我的迴應的HttpError內容:
b'{\n "error": {\n "code": 500,\n "message": "Internal error
encountered.",\n "errors": [\n {\n "message": "Internal error
encountered.",\n "domain": "global",\n "reason":
"backendError"\n }\n ],\n "status": "INTERNAL"\n }\n}\n'
歡迎訪問谷歌API張用OAuth2用戶端ID到堆棧溢出!你應該解釋更多關於你的問題,你已經嘗試/完成了什麼,並嘗試更具體。請看[如何提出一個好問題](https://stackoverflow.com/help/how-to-ask) –
對不起,我添加了一些關於我的調查的更多信息 – Paulus