如何在Apps Script中使用OAuth的signature_type?

huangapple go评论66阅读模式
英文:

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_URLCLIENT_KEYCLIENT_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, and CLIENT_SECRET, again.

Reference:

huangapple
  • 本文由 发表于 2023年4月11日 08:50:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定