通过Oauth2向Google App脚本Web App发送HTTP请求

编程入门 行业动态 更新时间:2024-10-28 02:31:27
本文介绍了通过Oauth2向Google App脚本Web App发送HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述
  • 列表项
  • 我创建了一个适用于HTTP请求的Google App脚本REST-应用程序(以"script.google/"开头).

    I have created a Google App Script REST - Application (starting with "script.google/"), 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.

    这两种方法都使我获得了有效的授权令牌,可以将其复制+粘贴到另一个平台,并确认它是否正在处理对 wwww.googlapis/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 wwww.googlapis/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 = "script.google/[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 = "script.google/[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 = "[script.google/[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".

    对于我设置的范围:

    www.googleapis/auth/plus.me www.googleapis/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.

  • 我认为我的第一次尝试涵盖了这个.
  • 我试图将Java答案转换为C#
  • I think my first attempt covers this one.
  • 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中www.googleapis/auth/drive.file范围授权的访问令牌一起使用.

    I was able to get this to work with an access token authorized with the www.googleapis/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请求

    本文发布于:2023-10-12 00:27:10,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1483266.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:脚本   Google   App   HTTP   Web

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!