401 未经授权使用谷歌任务

发布于 2024-12-21 12:26:10 字数 1735 浏览 1 评论 0原文

我正在学习使用 google 任务 api 休息。

我已经成功获得访问令牌。

现在我尝试使用此休息网址获取任务列表: https://www.googleapis.com/tasks/v1/users/@me/lists" googleapis.com/tasks/v1/users/@me/lists

在谷歌任务文档中写道,我需要通过 HTTP 授权标头发送访问令牌,但我不知道如何 链接

我在网络上搜索过结果,但没有找到了解决方案。 我也在这个网站上搜索过解决方案,但没有找到。

我在尝试 request.GetResponse(); 时收到 401 未经授权的错误 这是我的代码

 private AccessToken _accessToken = null;
private string Apikey = "my api key";
protected void Page_Load(object sender, EventArgs e)
{
    _accessToken = (AccessToken)Session["AccessTokken"];
    string _customerkey = "my customer key";
    string _customerSecret = "my customer secret key";
    Response.Write(_accessToken.Token);

    string nostring = "";
    string nnString = "";
    OAuthBase oauth = new OAuthBase();
    Uri t = new Uri("https://www.googleapis.com/tasks/v1/users/@me/lists");
    string u = oauth.GenerateSignature(t, _customerkey, _customerSecret, _accessToken.Token,
                                       _accessToken.TokenSecret, "GET", oauth.GenerateTimeStamp(),
                                       oauth.GenerateNonce(), out nostring, out nnString);

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(nostring);
    request.Method = "GET";

    //request.Headers.Add("Authorization: Bearer " + _accessToken.Token);
    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string responseString = reader.ReadToEnd();
    reader.Close();


    Response.Write(responseString);

I'm learning to use google task api rest.

i have succeed getting access tokken.

now i am trying to get tasklist using this rest url: https://www.googleapis.com/tasks/v1/users/@me/lists

In google task documentation write that i need to sent Access tokens by HTTP Authorization header but i don't know how
link

i have search for result in the web but didn't found a solution.
I have also searched this site for solution, but didn't get one.

i am getting 401 unauthorized error in when trying to request.GetResponse();
this is my code

 private AccessToken _accessToken = null;
private string Apikey = "my api key";
protected void Page_Load(object sender, EventArgs e)
{
    _accessToken = (AccessToken)Session["AccessTokken"];
    string _customerkey = "my customer key";
    string _customerSecret = "my customer secret key";
    Response.Write(_accessToken.Token);

    string nostring = "";
    string nnString = "";
    OAuthBase oauth = new OAuthBase();
    Uri t = new Uri("https://www.googleapis.com/tasks/v1/users/@me/lists");
    string u = oauth.GenerateSignature(t, _customerkey, _customerSecret, _accessToken.Token,
                                       _accessToken.TokenSecret, "GET", oauth.GenerateTimeStamp(),
                                       oauth.GenerateNonce(), out nostring, out nnString);

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(nostring);
    request.Method = "GET";

    //request.Headers.Add("Authorization: Bearer " + _accessToken.Token);
    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string responseString = reader.ReadToEnd();
    reader.Close();


    Response.Write(responseString);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2024-12-28 12:26:10

Google 发布了一个 .Net 库 用于访问 Google Api,这包括用于进行身份验证的帮助程序,我建议使用此 Api 并从 示例项目发布者(包括任务 Api 示例)。

如果您想手动启动 Api 调用,您可以使用 通信库,或者查看 源代码< /a> 看看 Google 是如何进行通信的。

编辑

从上面链接的源中您可以找到 OAuth 验证器 添加 http 标头 System.Net.HttpRequestHeader.Authorization 以及 < 中指定的值一个href="http://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/Apis/Oauthutil.cs?r=5b63689fcb1ec74a145f2d03b2ad103aa1526afc#68" rel="nofollow">OAuthUtil .GenerateHeader。相关部分复制如下。

StringBuilder sb = new StringBuilder();
sb.Append("Authorization: OAuth oauth_version=\"1.0\",");
sb.AppendFormat("oauth_nonce=\"{0}\",", EncodingPerRFC3986(nonce));
sb.AppendFormat("oauth_timestamp=\"{0}\",", EncodingPerRFC3986(timeStamp));
sb.AppendFormat("oauth_consumer_key=\"{0}\",", EncodingPerRFC3986(consumerKey));
if (!String.IsNullOrEmpty(token))
{
    sb.AppendFormat("oauth_token=\"{0}\",", EncodingPerRFC3986(
}
sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
sb.AppendFormat("oauth_signature=\"{0}\"", EncodingPerRFC3986(signature));

return sb.ToString();

您可以只使用 Api 的底层,而不必担心生成的服务。请记住,此代码由 http://www.apache.org/licenses/LICENSE-2.0 因此,从法律上讲,使用 DLL 来实现此特定功能可能比直接处理它更容易。

There is a Google publishes a .Net library for accessing Google Api, this includes helpers for doing the authentication I would suggest using this Api and starting from the samples the project publishers (including Task Api Sample).

If you want to carry on hand cranking the Api call you can either just use the communications library, or look at the source code and see how Google has done the communication.

EDIT

From the source linked above you can find the OAuth Authenticator which adds http header System.Net.HttpRequestHeader.Authorization with the value specified in OAuthUtil.GenerateHeader. The relevant section copied below.

StringBuilder sb = new StringBuilder();
sb.Append("Authorization: OAuth oauth_version=\"1.0\",");
sb.AppendFormat("oauth_nonce=\"{0}\",", EncodingPerRFC3986(nonce));
sb.AppendFormat("oauth_timestamp=\"{0}\",", EncodingPerRFC3986(timeStamp));
sb.AppendFormat("oauth_consumer_key=\"{0}\",", EncodingPerRFC3986(consumerKey));
if (!String.IsNullOrEmpty(token))
{
    sb.AppendFormat("oauth_token=\"{0}\",", EncodingPerRFC3986(
}
sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
sb.AppendFormat("oauth_signature=\"{0}\"", EncodingPerRFC3986(signature));

return sb.ToString();

You can use just this bottom layer of the Api and not worry about the generated services. Remember this code is covered by http://www.apache.org/licenses/LICENSE-2.0 so it may be easier in a legal seance to use the DLL for this specific function rather then coping it directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文