带有 Oauth2 的 HTTP 请求到 Google App Script Web App [英] HTTP Request with Oauth2 to Google App Script Web App

查看:18
本文介绍了带有 Oauth2 的 HTTP 请求到 Google App Script Web App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 列表项

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

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

当所有人,甚至匿名"都可以使用该应用程序时,它可以正常工作,但是当我将它设置为仅对我的域可用 或仅我自己"从发布/部署为 WebApp[/编辑] 时,我只能使用浏览器和登录访问 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) Unauthorized".

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#

好的,感谢您阅读这里,希望有人能帮我解决这个问题,因为我的想法(和时间,最终)用完了.

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.

推荐答案

我能够使用通过 https://www.googleapis.com/auth/drive.file 授权的访问令牌来使用它 Google OAuth Playground 中的范围.

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 的 HTTP 请求到 Google App Script Web App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆