从回调中获取oauth_verifier

时间:2012-12-03 15:41:37

标签: c# oauth verification

我已经搜索了这个问题的答案。基本上,我正在创建一个C#应用程序(在它的第一个版本中)将使用使用OAuth 1.0a的Projectplace API进行身份验证。它当前将oauth_verifier返回到地址栏,但是当我使用var response = request.GetResponse();方法时,它会返回我作为授权的一部分首先发送的oauth_tokenoauth token_secret。< / p>

也许我误解了这个过程的运作方式,但我已经阅读了每一个答案,似乎都没有解决这个问题。在我在加载回调URL后在身份验证页面上输入用户名和密码后,您是否必须或者是否可以从地址栏(或其他任何可以获取的地方)中提取验证者代码?

我认为OAuth1.0a要求验证码检索访问令牌,我找不到一种简单的方法来提取验证码。

我真的很感激任何帮助,这让我疯了!

更新于03.12.12

感谢您的回复!

基本上,我是在下面发送此初始请求后尝试从oauth提供程序检索oauth_verifier的客户端,我的下一步是授权然后检索验证程序。我按照你的建议尝试了下面的内容,比如在这里深入游泳:)

//Generate string for initiation request.
 requestUri.AppendFormat("?oauth_consumer_key={0}&", consumerKey);
 requestUri.AppendFormat("oauth_nonce={0}&", nonce);
 requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
 requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
 requestUri.AppendFormat("oauth_version={0}&", "1.0");
 requestUri.AppendFormat("oauth_signature={0}", signature);
 var request = (HttpWebRequest)WebRequest.Create(new Uri(requestUri.ToString()));
 request.Method = WebRequestMethods.Http.Get;
 var response = request.GetResponse();
 var queryString = new StreamReader(response.GetResponseStream()).ReadToEnd();
 var parts = queryString.Split('&');
 var token = parts[1].Substring(parts[1].IndexOf('=') + 1);
 var tokenSecret = parts[0].Substring(parts[0].IndexOf('=') + 1);
 var queryString2 = String.Format("oauth_token={0}", token);

//AUTHORIZE WITH CREDENTIALS FROM USER.
 var authorizeUrl = "https://api.projectplace.com/authorize?" + queryString;
 Process.Start(authorizeUrl);`

//TRY AND READ VERIFICATION STRING AFTER AUTHORIZATION REDIRECT`
 String oauthVerifier = HttpContext.Current.Request.QueryString["oauth_verifier"];

不幸的是,一旦我完成了这个,我似乎无法返回一个查询字符串,显示我在地址栏中显示的字符串中清楚看到的oauth_verifier。 (是的,这是一种非常新的描述方式,我正在学习代码以及OAuth:P)。

到目前为止,感谢您的帮助。我试图运行上面的内容,但它只是说“对象引用没有设置为对象的实例。”。

另外,如果我尝试使用之前用于获取查询字符串/响应的代码?从使用以下行的启动请求开始,querystring3只是空白回来......真的很令人沮丧! :)

var queryString3 = new StreamReader(response.GetResponseStream()).ReadToEnd(); var parts3 = queryString3.Split('&');

1 个答案:

答案 0 :(得分:0)

我将假设“在地址栏中”表示oauth_verifier通过重定向URL中的查询字符串参数从ProjectPlace服务器传递到您的站点。为了在C#服务器端代码中读取它,您将使用类似下面的内容(我修改了此解决方案的示例代码):

 requestUri.AppendFormat("?oauth_consumer_key={0}&", consumerKey);
 requestUri.AppendFormat("oauth_nonce={0}&", nonce);
 requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
 requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
 requestUri.AppendFormat("oauth_version={0}&", "1.0");
 requestUri.AppendFormat("oauth_signature={0}", signature);
 var request = (HttpWebRequest)WebRequest.Create(new Uri(requestUri.ToString()));

 //Note: this is unnecessary - GET is the default
 request.Method = WebRequestMethods.Http.Get; 
 //By casting to HttpWebResponse you get access to the QueryString property
 var response = request.GetResponse() as HttpWebResponse;
 var oauthVerifier = response.QueryString["oauth_verifier"];

//The response stream contains the HTTP response body, 
//which will not contain the URL to which the redirect is sent
//I'm not sure if there is anything there that you will need
var responseBody = new StreamReader(response.GetResponseStream()).ReadToEnd();

 //AUTHORIZE WITH CREDENTIALS FROM USER.  -- Not sure what this section is doing
 var queryString = string.Format("oauth_token={0}", oauthVerifier);
 var authorizeUrl = "https://api.projectplace.com/authorize?" + queryString;
 Process.Start(authorizeUrl);