英文:
How to use OAuth signature_type in Apps Script?
问题
以下是您要翻译的Python代码部分:
from requests_oauthlib import OAuth1
BASE_URL = '...'
CLIENT_KEY = '...'
CLIENT_SECRET = '...'
def get_oauth_query():
"""获取OAuth查询"""
queryoauth = OAuth1(CLIENT_KEY, CLIENT_SECRET, signature_type="query")
return queryoauth
url = BASE_URL + '/user/v2/credentials/'
queryoauth = OAuth1(CLIENT_KEY, CLIENT_SECRET, signature_type="query")
request = requests.get(url, auth=get_oauth_query())
content = request.json()
credentialId = content[0]["id"]
print(credentialId)
请注意,代码中的HTML实体(如"
)已被还原为正常的引号。如果需要将此Python脚本转换为Google Apps Script,请参考您提供的OAuth脚本链接中的示例,然后根据您的需求进行修改。
英文:
I have the following OAuth authentication script in Python that works for the api. I want to convert it to run in Google Apps Script using the signature_type='query'
. How should I do it?
from requests_oauthlib import OAuth1
BASE_URL = '...'
CLIENT_KEY = '...'
CLIENT_SECRET = '...'
def get_oauth_query():
""" Get OAuth Query """
queryoauth = OAuth1(CLIENT_KEY, CLIENT_SECRET, signature_type="query")
return queryoauth
url = BASE_URL + '/user/v2/credentials/'
queryoauth = OAuth1(CLIENT_KEY, CLIENT_SECRET, signature_type="query")
request = requests.get(url, auth=get_oauth_query())
content = request.json()
credentialId = content[0]["id"]
print(credentialId)
P.S.: I cannot use Library service on the work laptop, but I can copy the following OAuth script into my project.
答案1
得分: 1
在signature_type="query"
的情况下,这些值将被用作查询参数。根据您的问题,当您想要在OAuth1 for Apps Script中使用时,您可以将您的Python脚本转换如下:
在这个示例脚本中,假定使用OAuth1 for Apps Script。请注意这一点。
修改后的脚本:
function myFunction() {
const BASE_URL = "...";
const CLIENT_KEY = "...";
const CLIENT_SECRET = "...";
const service = OAuth1.createService("sample")
.setConsumerKey(CLIENT_KEY)
.setConsumerSecret(CLIENT_SECRET)
.setParamLocation("uri-query")
.setAccessToken("", "");
const url = BASE_URL + "/user/v2/credentials/";
const response = service.fetch(url);
// console.log(response.getContentText()); // You can see the raw response value using this line.
const content = JSON.parse(response.getContentText());
const credentialId = content[0]["id"];
console.log(credentialId);
}
-
从您的Python脚本中的
signature_type="query"
,可以使用setParamLocation("uri-query")
。 -
我认为这个请求与您展示的Python脚本相同。但如果出现错误,请再次确认您的
BASE_URL
、CLIENT_KEY
和CLIENT_SECRET
的值。
参考:
英文:
I thought that in the case of signature_type="query"
, the values are used as the query parameter. From your question, when you want to use OAuth1 for Apps Script, how about converting your Python script as follows?
In this sample script, it supposes that OAuth1 for Apps Script is used. Please be careful about this.
Modified script:
function myFunction() {
const BASE_URL = "...";
const CLIENT_KEY = "...";
const CLIENT_SECRET = "...";
const service = OAuth1.createService("sample")
.setConsumerKey(CLIENT_KEY)
.setConsumerSecret(CLIENT_SECRET)
.setParamLocation("uri-query")
.setAccessToken("", "");
const url = BASE_URL + "/user/v2/credentials/";
const response = service.fetch(url);
// console.log(response.getContentText()); // You can see the raw response value using this line.
const content = JSON.parse(response.getContentText());
const credentialId = content[0]["id"];
console.log(credentialId);
}
-
From
signature_type="query"
of your Python script,setParamLocation("uri-query")
is used. -
I think that this request is the same as your showing Python script. But, if an error occurs, please confirm your values of
BASE_URL
,CLIENT_KEY
, andCLIENT_SECRET
, again.
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论