本文介绍了通过Oauth2向Google App脚本Web App发送HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 列表项

我创建了一个适用于HTTP请求的Google App脚本REST-应用程序(以"script.google.com/"开头).

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

该应用程序对所有人(甚至匿名)都可用"时工作正常,但是当我将其设置为仅对我的域可用时或从发布/部署"为WebApp [/EDIT]的仅我自己",我只能使用浏览器和登录访问Web应用,而不能使用http请求访问.

The application works fine when it is available to 'everyone, even anonymous' but when I set it available to my domain only 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.

我尝试通过Google OAuth Playground 和基于 Xamarin认证教程.

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

这两种方法都使我获得了有效的授权令牌,可以将其复制+粘贴到另一个平台,并确认它是否正在处理对 https://wwww.googlapis.com/plus/v1/people/me .

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.

我可以使用浏览器访问Web应用程序并登录.现在,当我使用http请求调用脚本时,将得到以下结果:

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>"

我尝试使用另一个应用程序脚本来调用Web应用程序:

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

我还尝试使用C#OAuth2Request调用Web应用程序(来自Xamarin教程):

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

我也尝试过C#HttpWebRequest:

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();

所有以前的方法都具有相同的结果:(401)未经授权".

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

对于我设置的范围:

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

我的WebApp根据其属性不需要任何范围.还要确保没有,我没有设置一个尽可能简单的doGet()方法:

My WebApp does not require any scopes according to it's properties. 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. 我认为我的第一次尝试涵盖了这个.
  2. 我试图将Java答案转换为C#
  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).

尽管该问题已解决并证明是范围问题,但我还是在下面的评论中回答这些问题,以防万一该问题将来对任何人都有帮助.

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.

推荐答案

我能够将其与Google OAuth Playground中https://www.googleapis.com/auth/drive.file范围授权的访问令牌一起使用.

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.

这篇关于通过Oauth2向Google App脚本Web App发送HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 11:29