获取雅虎联系人列表中的C#应用程序 [英] Get Yahoo Contact List in C# application
问题描述
作为指导 https://developer.yahoo.com/oauth/guide /oauth-requesttoken.html ,下面是我做了什么通过OAuth获得雅虎联系人列表:
- 请GET请求到 https://api.login.yahoo.com/oauth/v2/get_request_token
获得组oauth_token和oauth_token_secret,我成功地让他们
示例网址:
OAuthBase誓=新OAuthBase();
字符串URL =https://api.login.yahoo.com/oauth/v2/get_request_token? +
oauth_nonce =+ oath.GenerateNonce()+
及放大器; oauth_timestamp =+ oath.GenerateTimeStamp()+
及放大器; oauth_consumer_key =+ consumerKey +
及放大器; oauth_signature_method =明文+
及放大器; oauth_signature =+ consumerSecret +%26+ //%26,如果明文
及放大器; oauth_version = 1.0+
&放大器; oauth_callback =+OOB;
-
使用步骤1中的组oauth_token。做一个GET请求 https://api.login.yahoo.com/ OAuth的/ V2 / request_auth?组oauth_token = {令牌},这回我oauth_verifier
-
使用步骤1和2的paramters使GET请求 https://api.login.yahoo.com/oauth/v2/get_token ,它返回我新组oauth_token,oauth_token_secret,oauth_session_handle,xoauth_yahoo_guid
示例网址:
字符串SIG = consumerSecret +%26+ OauthTokenSecret;
字符串URL =https://api.login.yahoo.com/oauth/v2/get_token? +
oauth_consumer_key =+ consumerKey +
及放大器; oauth_signature_method =明文+
及放大器; oauth_signature =+ SIG +
及放大器; oauth_timestamp = + oath.GenerateTimeStamp()+
及放大器; oauth_version = 1.0+
及放大器;组oauth_token =+ OAuthToken +
及放大器; oauth_nonce =+ oath.GenerateNonce()+
&放大器; oauth_verifier =+ oauth_verifier;
<醇开始=4>
我做一个GET请求 https://social.yahooapis.com/v1/user / {} xoauth_yahoo_guid /联系人?格式= JSON
示例
开放的URI =新的URI(URL);
串随机数= oath.GenerateNonce();
串的timeStamp = oath.GenerateTimeStamp();
串normalizedUrl;
串normalizedRequestParameters;
串SIG = oath.GenerateSignature(URI,客户端ID,clientSecret,yahooToken.OAuthToken,
yahooToken.OauthTokenSecret,GET,时间戳,现时,OAuthBase.SignatureTypes.HMACSHA1,
OUT normalizedUrl,出normalizedRequestParameters);
功能GenerateSignature:
公共字符串GenerateSignature(URI链接,串consumerKey,串consumerSecret,字符串标记,字符串tokenSecret,串列举HTTPMethod,字符串的timeStamp,串现时,SignatureTypes signatureType,出字符串normalizedUrl,出字符串normalizedRequestParameters)
{
normalizedUrl = NULL;
normalizedRequestParameters = NULL;
开关(signatureType)
{
情况下SignatureTypes.PLAINTEXT:
返回HttpUtility.UrlEncode(的String.Format({0}&放大器; {1}, consumerSecret,tokenSecret));
情况下SignatureTypes.HMACSHA1:
串signatureBase = GenerateSignatureBase(URL,consumerKey,令牌,tokenSecret,列举HTTPMethod,时间戳,现时,HMACSHA1SignatureType,出normalizedUrl,出normalizedRequestParameters);
HMACSHA1 HMACSHA1 =新HMACSHA1();
hmacsha1.Key = Encoding.ASCII.GetBytes(的String.Format({0}&放大器; {1},以UrlEncode(consumerSecret),string.IsNullOrEmpty(tokenSecret):以UrlEncode(tokenSecret))) ;
返回GenerateSignatureUsingHash(signatureBase,HMACSHA1);
情况下SignatureTypes.RSASHA1:
抛出新NotImplementedException();
默认:
抛出新的ArgumentException(未知的签名类型,signatureType);
}
}
请要求:
使用(VAR的客户=新的WebClient())
{
串authenHeader =OAuth的+
的境界= \\ \\yahooapis.com\+
,oauth_consumer_key = \+ consumerKey +\+
,oauth_nonce = \+ +随机数\ +
,oauth_signature_method = \HMAC-SHA1\+
,oauth_timestamp = \+ +的timeStamp\+
组oauth_token = \\ \\+ OAuthToken +\+
,oauth_version = \1.0\+
,oauth_signature = \+ HttpUtility.UrlEncode(SIG)+ \;
client.Headers.Set(授权,authenHeader);
串responseString = client.DownloadString(URL);
}
但雅虎给我发(401)未经授权的响应,你能告诉我在哪里'错了?
我意识到,我停留在完全相同的地方
System.Net.WebException:远程服务器返回错误:(401)未经授权。在...
私人的ArrayList GetEmailContacts(){
OAuthBase的OAuth =新OAuthBase ..GetEmailContacts()( );
乌里URI =新的URI(https://social.yahooapis.com/v1/user/+ OauthYahooGuid +/联系人格式的XML =?);
串随机数= oauth.GenerateNonce();
串的timeStamp = oauth.GenerateTimeStamp();
串normalizedUrl;
串normalizedRequestParameters;
串SIG = oauth.GenerateSignature(URI,this.sConsumerKey,this.sConsumerSecret,OauthToken,OauthTokenSecret,GET,时间戳,现时,OAuthBase.SignatureTypes.HMACSHA1,出normalizedUrl,出normalizedRequestParameters);
StringBuilder的sbGetContacts =新的StringBuilder(uri.ToString());
尝试{
串returnStr =的String.Empty;
HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(sbGetContacts.ToString());
req.Accept =application / xml进行;
req.Method =GET;
ArrayList的电子邮件=新的ArrayList();
串authHeader =授权:OAuth的+
的境界= \yahooapis.com\+
,oauth_consumer_key = \+这一点。 sConsumerKey +\+
,oauth_nonce = \+ +随机数\+
,oauth_signature_method = \HMAC-SHA1\+
,oauth_timestamp = \+ +的timeStamp\+
组oauth_token = \+ OauthToken +\+
,oauth_version = \ 1.0\+
,oauth_signature = \+ HttpUtility.UrlEncode(SIG)+\;
req.Headers.Add(authHeader);
使用(HttpWebResponse RES =(HttpWebResponse)req.GetResponse()){
:/ sighz
SO,我发现这个问题。香港专业教育学院改变了内容格式从XML到JSON
改变了以下内容:
私人的ArrayList GetEmailContacts(){
OAuthBase的OAuth =新OAuthBase();
乌里URI =新的URI(https://social.yahooapis.com/v1/user/+ OauthYahooGuid +/联系人格式= JSON?);
串随机数= oauth.GenerateNonce();
串的timeStamp = oauth.GenerateTimeStamp();
串normalizedUrl;
串normalizedRequestParameters;
串SIG = oauth.GenerateSignature(URI,this.sConsumerKey,this.sConsumerSecret,OauthToken,OauthTokenSecret,GET,时间戳,现时,OAuthBase.SignatureTypes.HMACSHA1,出normalizedUrl,出normalizedRequestParameters);
StringBuilder的sbGetContacts =新的StringBuilder(uri.ToString());
尝试{
串returnStr =的String.Empty;
HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(sbGetContacts.ToString());
req.Accept =应用/ JSON;
req.ContentType =应用/ JSON;
req.Method =GET;
ArrayList的电子邮件=新的ArrayList();
串authHeader =授权:OAuth的+
的境界= \yahooapis.com\+
,oauth_consumer_key = \+这一点。 sConsumerKey +\+
,oauth_nonce = \+ +随机数\+
,oauth_signature_method = \HMAC-SHA1\+
,oauth_timestamp = \+ +的timeStamp\+
组oauth_token = \+ OauthToken +\+
,oauth_version = \ 1.0\+
,oauth_signature = \+ HttpUtility.UrlEncode(SIG)+\;
req.Headers.Add(authHeader);
使用(HttpWebResponse RES =(HttpWebResponse)req.GetResponse()){
当我提出,改变它突然返回的电子邮件,并没有更多的406错误代码。
我希望这可以帮助别人..不知道,为什么XML停止工作
As guidance on https://developer.yahoo.com/oauth/guide/oauth-requesttoken.html, Below are what I did with OAuth to get Yahoo contact list:
- Make a GET request to https://api.login.yahoo.com/oauth/v2/get_request_token to get oauth_token and oauth_token_secret, I successfully get them.
Example url:
OAuthBase oath = new OAuthBase();
string url = "https://api.login.yahoo.com/oauth/v2/get_request_token?" +
"oauth_nonce=" + oath.GenerateNonce() +
"&oauth_timestamp=" + oath.GenerateTimeStamp() +
"&oauth_consumer_key=" + consumerKey+
"&oauth_signature_method=plaintext" +
"&oauth_signature=" + consumerSecret + "%26" + //%26 if plaintext
"&oauth_version=1.0" +
"&oauth_callback=" + "oob";
Using oauth_token of step 1. to make a GET request to https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token={token}, It returned me oauth_verifier
Using paramters of step 1 and 2 to make GET request to https://api.login.yahoo.com/oauth/v2/get_token , it returned me new oauth_token , oauth_token_secret, oauth_session_handle, xoauth_yahoo_guid.
Example url:
string sig = consumerSecret + "%26" + OauthTokenSecret;
string url = "https://api.login.yahoo.com/oauth/v2/get_token?" +
"oauth_consumer_key=" + consumerKey+
"&oauth_signature_method=plaintext" +
"&oauth_signature=" + sig +
"&oauth_timestamp=" + oath.GenerateTimeStamp() +
"&oauth_version=1.0" +
"&oauth_token=" + OAuthToken +
"&oauth_nonce=" + oath.GenerateNonce() +
"&oauth_verifier=" + oauth_verifier;
- The final step is to get the contact list: I make a GET request to https://social.yahooapis.com/v1/user/{xoauth_yahoo_guid}/contacts?format=json
Example
Uri uri = new Uri(url);
string nonce = oath.GenerateNonce();
string timeStamp = oath.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;
string sig = oath.GenerateSignature(uri, clientId, clientSecret, yahooToken.OAuthToken,
yahooToken.OauthTokenSecret, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1,
out normalizedUrl, out normalizedRequestParameters);
Function GenerateSignature:
public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters)
{
normalizedUrl = null;
normalizedRequestParameters = null;
switch (signatureType)
{
case SignatureTypes.PLAINTEXT:
return HttpUtility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret));
case SignatureTypes.HMACSHA1:
string signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters);
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));
return GenerateSignatureUsingHash(signatureBase, hmacsha1);
case SignatureTypes.RSASHA1:
throw new NotImplementedException();
default:
throw new ArgumentException("Unknown signature type", "signatureType");
}
}
Make request:
using (var client = new WebClient())
{
string authenHeader = "OAuth " +
"realm=\"yahooapis.com\"" +
",oauth_consumer_key=\"" + consumerKey+ "\"" +
",oauth_nonce=\"" + nonce + "\"" +
",oauth_signature_method=\"HMAC-SHA1\"" +
",oauth_timestamp=\"" + timeStamp + "\"" +
",oauth_token=\"" + OAuthToken + "\"" +
",oauth_version=\"1.0\"" +
",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\"";
client.Headers.Set("Authorization", authenHeader);
string responseString = client.DownloadString(url);
}
But Yahoo sends me (401) Unauthorized response, Could you tell me where I'm wrong ?
I realized that I am stuck at the exact same place
System.Net.WebException: The remote server returned an error: (401) Unauthorized. at ..GetEmailContacts() in ...
private ArrayList GetEmailContacts() {
OAuthBase oauth = new OAuthBase();
Uri uri = new Uri("https://social.yahooapis.com/v1/user/" + OauthYahooGuid + "/contacts?format=XML");
string nonce = oauth.GenerateNonce();
string timeStamp = oauth.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;
string sig = oauth.GenerateSignature(uri, this.sConsumerKey, this.sConsumerSecret, OauthToken, OauthTokenSecret, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);
StringBuilder sbGetContacts = new StringBuilder(uri.ToString());
try {
string returnStr = string.Empty;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sbGetContacts.ToString());
req.Accept = "application/xml";
req.Method = "GET";
ArrayList emails = new ArrayList();
string authHeader = "Authorization: OAuth " +
"realm=\"yahooapis.com\"" +
",oauth_consumer_key=\"" + this.sConsumerKey + "\"" +
",oauth_nonce=\"" + nonce + "\"" +
",oauth_signature_method=\"HMAC-SHA1\"" +
",oauth_timestamp=\"" + timeStamp + "\"" +
",oauth_token=\"" + OauthToken + "\"" +
",oauth_version=\"1.0\"" +
",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\"";
req.Headers.Add(authHeader);
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) {
:/ sighz
SO I found the issue. Ive changed the content format from XML to JSON
changed the following:
private ArrayList GetEmailContacts() {
OAuthBase oauth = new OAuthBase();
Uri uri = new Uri("https://social.yahooapis.com/v1/user/" + OauthYahooGuid + "/contacts?format=json");
string nonce = oauth.GenerateNonce();
string timeStamp = oauth.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;
string sig = oauth.GenerateSignature(uri, this.sConsumerKey, this.sConsumerSecret, OauthToken, OauthTokenSecret, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);
StringBuilder sbGetContacts = new StringBuilder(uri.ToString());
try {
string returnStr = string.Empty;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sbGetContacts.ToString());
req.Accept = "application/json";
req.ContentType = "application/json";
req.Method = "GET";
ArrayList emails = new ArrayList();
string authHeader = "Authorization: OAuth " +
"realm=\"yahooapis.com\"" +
",oauth_consumer_key=\"" + this.sConsumerKey + "\"" +
",oauth_nonce=\"" + nonce + "\"" +
",oauth_signature_method=\"HMAC-SHA1\"" +
",oauth_timestamp=\"" + timeStamp + "\"" +
",oauth_token=\"" + OauthToken + "\"" +
",oauth_version=\"1.0\"" +
",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\"";
req.Headers.Add(authHeader);
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) {
When I made that change it suddenly returned the emails and no more 406 error code.
I hope that this helps someone.. no idea as to why xml stopped working.
这篇关于获取雅虎联系人列表中的C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!