1

I want to change python to apps script in apps script have UrlFetchApp function i'm never use but i'm try
I have code python can get api normally

import requests
 
url = "https://api.aiforthai.in.th/ssense"
 
text = 'Have a good day'
 
params = {'text':text}
 
headers = {
    'Apikey': "xxx-xxx-xxx"
    }
 
response = requests.get(url, headers=headers, params=params)
 
print(response.json())

so now i'm try code apps script like this but notthing came out ; Api dashboard call me i'm use api.
maybe wrong payload text?

Detail API

This my wrong apps script code

function call_api() {
  var url = "https://api.aiforthai.in.th/ssense"

  var apiKey = "xxx-xxx-xxx";

  var response = UrlFetchApp.fetch(
    url,
    {
      "headers": {
        "Apikey": apiKey,
        "text": "Have a good day"
      }
    }
  )
  Logger.log(response)
}

Thank you for solution.

2 Answers 2

1

I believe your goal is as follows.

  • You want to convert the following python script to Google Apps Script.

      import requests
    
      url = "https://api.aiforthai.in.th/ssense"
    
      text = 'Have a good day'
    
      params = {'text':text}
    
      headers = {
          'Apikey': "xxx-xxx-xxx"
          }
    
      response = requests.get(url, headers=headers, params=params)
    
      print(response.json())
    
  • You have already been confirmed that your python script worked fine.

When I saw your python script, text is sent as the query parameter. In this case, how about the folloiwng modification?

Modified script:

function call_api2() {
  var text = "Have a good day";
  var url = `https://api.aiforthai.in.th/ssense?text=${encodeURIComponent(text)}`;
  var apiKey = "xxx-xxx-xxx";
  var response = UrlFetchApp.fetch(
    url,
    { "headers": { "Apikey": apiKey } }
  );
  Logger.log(response.getContentText());
}

Note:

  • If you test the above modified script, when an error occurs, please confirm your apiKey again.
  • If an error like status code 403 occurs, your URL might not be able to be requested from Google side. I'm worried about this.
Sign up to request clarification or add additional context in comments.

Comments

0

Try this instead:

function call_api() {
  var url = "https://api.aiforthai.in.th/ssense";

  var apiKey = "xxx-xxx-xxx";

  var response = UrlFetchApp.fetch(
    url,
    {
      "method" : "GET",
      "headers" : {
        "Apikey" : apiKey,
        "text" : "Have a good day"
      }
    }
  );
  Logger.log(response)
}

You can check out the UrlFetchApp documentation for future reference.

3 Comments

can run and call api but not show anything
@wato What is the output when running your Python code? And do you have any documentation related to the API you're trying to interact with?
json pattern , that's sure i have doc api but doc have tutorial python no one apps script and i try call in apps script because i try call with python it's work T_T // If your have new method can you tell me because i will call api in google sheet and import data to google data studio. Thank you sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.