Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request from server to Google Web App returns 302 code

Has anyone call a web app written in Google App script from Server (Rails...).

The image below describes exactly my problem. When I call GAS project from Server, it always returns 302 status. But it's ok when I call GAS directly

Image

I run this code alone (Ruby), response status was 200, but when I put this code into Rails project, Google App Script returned 302

require 'faraday'

spreasheet_url = 'xxx'
token = 'xxx'
conn = Faraday.new url: spreasheet_url
conn.authorization :Bearer, token
res = conn.post do |req|
  req.headers['Content-Type'] = 'application/json'
  req.body = data.to_json
end

p res.status

I have tried with Nodejs and Go and the same problem happened.

Please help me if you have any suggestions, thanks a lot!

like image 348
Tran B. V. Son Avatar asked Aug 27 '26 07:08

Tran B. V. Son


1 Answers

For security reasons, content returned by the Content service isn’t served from script.google.com, but instead redirected to a one-time URL at script.googleusercontent.com. This means that if you use the Content service to return data to another application, you must ensure that the HTTP client is configured to follow redirects. For example, in the cURL command line utility, add the flag -L. Check the documentation for your HTTP client for more information on how to enable this behavior.

Content Service | Apps Script | Google Developers

If you use Ruby (with Faraday gem), the solution is you should add FollowRedirects middleware:

conn = Faraday.new(url: spreasheet_url) do |faraday|
  faraday.use FaradayMiddleware::FollowRedirects
  faraday.adapter Faraday.default_adapter
end

The idea same as Go or Nodejs.

like image 177
Tran B. V. Son Avatar answered Aug 31 '26 13:08

Tran B. V. Son



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!