google-apps-scriptoauth

How to use OAuth signature_type in Apps Script?


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.


Solution

  • 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);
    }
    

    Reference: