c# – Google Contacts API – 获取访问令牌(oauth)后
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Google Contacts API – 获取访问令牌(oauth)后,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2811字,纯文字阅读大概需要5分钟。
内容图文
![c# – Google Contacts API – 获取访问令牌(oauth)后](/upload/InfoBanner/zyjiaocheng/769/72d0469047cb416482e706b1dcafd985.jpg)
我设法为谷歌的联系人API获取访问令牌,但当我尝试拨打电话以检索登录用户的个人资料时,我收到401未经授权的错误…
我做了一些研究,并按照“各种”谷歌文档中提到的步骤(如this one和this one以及许多其他文档)但没有用…
到目前为止,我认为我正在签署错误的请求.这是我获得访问令牌后正在做的事情.
string outUrl,querystring;
string sig = oAuth.GenerateSignature(new Uri("https://www.google.com/m8/feeds/contacts/default/full"), Server.UrlEncode(oAuth.ConsumerKey), oAuth.ConsumerSecret, oAuth.Token, null, "GET", timeStamp, nonce, out outUrl, out querystring);
string reqURL = "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + oAuth.Token + "&oauth_signature_method=HMAC-SHA1&oauth_signature=" + Server.UrlEncode(sig) + "&oauth_consumer_key=" + oAuth.ConsumerKey + "&oauth_timestamp=" + timeStamp + "&oauth_nonce=" + nonce + "&oauth_version=1.0";
response = oAuth.WebRequest(oAuthGoogle.Method.GET, reqURL, String.Empty);
使用oAuth.WebRequest()发送请求时出现401错误(上面代码的最后一行)
我只需要摆脱401错误……我正在使用ASP.NET / C#.任何帮助,将不胜感激.谢谢…
解决方法:
您的代码示例定义了未使用的reqURL,并使用未定义的url.
您通常会使用授权标头而不是查询字符串提供OAuth请求参数.
http://oauth.net/core/1.0/#auth_header_authorization
我会想象签署请求并设置授权,这是在OAuth对象中处理的内容.
澄清
我使用这样的方法在我的OAuth 1.0a实现中签署http请求:
/// <summary>
/// Gets the authorization header.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="url">The URL of the request.</param>
/// <param name="parameters">The parameters.</param>
/// <returns>Authorization header</returns>
public string GetAuthorizationHeader(string method, Uri url, NameValueCollection parameters)
{
parameters.Set("oauth_consumer_key", this.ConsumerKey);
parameters.Set("oauth_nonce", this.GetNonce());
parameters.Set("oauth_timestamp", this.GetTimeStamp());
parameters.Set("oauth_version", "1.0");
parameters.Set("oauth_signature_method", "HMAC-SHA1");
string signString = this.GetSignString(method, url, parameters);
string signature = this.GetSignature(signString, this.ConsumerSecret, this.tokenSecret);
parameters.Set("oauth_signature", signature);
StringBuilder authorizationHeader = new StringBuilder();
foreach (string paramKey in parameters.AllKeys)
{
if (authorizationHeader.Length > 0)
{
authorizationHeader.Append(", ");
}
else
{
authorizationHeader.Append("OAuth ");
}
authorizationHeader.AppendFormat("{0}=\"{1}\"", paramKey, OAuthHelper.UrlEncode(parameters[paramKey]));
}
return authorizationHeader.ToString();
}
我这样用的
public void SignHttpWebRequest(string token, string tokenSecret, ref HttpWebRequest request)
{
NameValueCollection parameters = new NameValueCollection();
this.tokenSecret = tokenSecret;
parameters.Set("oauth_token", token);
request.Headers.Add("Authorization", this.GetAuthorizationHeader(request, parameters));
}
内容总结
以上是互联网集市为您收集整理的c# – Google Contacts API – 获取访问令牌(oauth)后全部内容,希望文章能够帮你解决c# – Google Contacts API – 获取访问令牌(oauth)后所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。