Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

c# - HTTP Request with Oauth2 to Google App Script Web App

  1. List item

I have created a Google App Script REST - Application (starting with "script.google.com/"), that works with HTTP-requests.

The application works fine when it is available to 'everyone, even anonymous' but when I set it available to my domain only [EDIT:] OR "only myself" from the publish/deploy as WebApp[/EDIT], I can only access the web app with browser and signing in but not with http request.

I have tried requesting an authorization token with both Google OAuth Playground and an android application based on a Xamarin Auth Tutorial.

Both methods have resulted me a working authorization token that I can copy+paste to an other platform an confirm it is working with a request to https://wwww.googlapis.com/plus/v1/people/me.

I can access the Web app with browser and signing in. Now when I call my script with http request I get the following result:

"<HTML> <HEAD> <TITLE>Unauthorized</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Unauthorized</H1> <H2>Error 401</H2> </BODY> </HTML>" 

I have tried to call the Web App with another App Script:

var headers =  {
    "authorization": "Bearer [access_token]",
  };   
  var params = {
     method:"GET",
     contentType:"application/json",
     muteHttpExceptions:true,
     headers:headers,    
  };
  var url = "https://script.google.com/[rest_of_the_script_url]";
  var response = UrlFetchApp.fetch(url, params);

Also I have tried calling the Web App with C# OAuth2Request (Taken from the Xamarin tutorial):

var url = "https://script.google.com/[rest_of_the_script_url]";
var request = new OAuth2Request("GET", new Uri(url), null, account );

Also I have tried C# HttpWebRequest:

string accessToken = "Bearer [access_token]";
string url = "[https://script.google.com/[rest_of_the_script_url]";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.create(url);

request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add("Authorization", accessToken);

var response = request.getResponse();

All previous methods have the same result: "(401) Unauthorized".

For scopes I have set:

https://www.googleapis.com/auth/plus.me 
https://www.googleapis.com/auth/userinfo.email

My WebApp does not require any scopes according to it's properties. [EDIT:] Also to make sure it does not I did set a doGet() method as simple as possible:

function doGet(e)
{
   return ContentService.CreateTextOutput("success");
}

This question has been asked before, but some have found the solution and some have not. Also I did not success with the answers either.

  1. I think my first attempt covers this one.
  2. I tried to translate the Java answer to C#

Ok, thanks for reading down here, wish some one can help me out with this as I'm running out of ideas (and time, eventually).

EDIT: Though the issue has resolved and turned out to be a scope-issue I am answering the questions in the comments below, in case this question might be of any help to anyone in the future.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to get this to work with an access token authorized with the https://www.googleapis.com/auth/drive.file scope in the Google OAuth Playground.

That doesn't seem quite right in my opinion, but it's the least permissive scope I got to work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...