我正在将Twitter集成添加到我公司的网络应用程序中,但我在其中遇到了障碍。
目前的问题在于回调网址。
在Twitter中,它似乎想要设定值,但我们的应用程序设置方式,访问该网站的每个客户都有自己的网址。
理想情况下,我希望动态设置回调网址,但我很难找到相关信息。
编辑: 想想我可能需要在这里添加更多细节,以便更好地帮助您
问题是我将回调网址参数设置为https://api.twitter.com/oauth/request_token但是当我转到https://api.twitter.com/oauth/authorize获取权限时,它会转到api上的回调设置,而不是我之前设置的回调设置请求。
这是我的代码:
public string oAuthToken(string callbackUrl)
{
Uri oauthUrl = new Uri("https://api.twitter.com/oauth/request_token");
var oauthNonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var oauthTimestamp = this.GenerateTimeStamp();
var oauthSignature = "";
var authSignature = string.Format("oauth_callback=\"{0}\"&oauth_consumer_key=\"{1}\"&oauth_nonce=\"{2}\"&oauth_signature_method=\"{3}\"&oauth_timestamp=\"{4}\"&oauth_version=\"{5}\"",
Uri.EscapeDataString(callbackUrl),
Uri.EscapeDataString(this.ConsumerKey),
Uri.EscapeDataString(oauthNonce),
Uri.EscapeDataString(oauthSignatureMethod),
Uri.EscapeDataString(oauthTimestamp),
Uri.EscapeDataString(oauthVersion));
var baseString = string.Format("POST&{0}&{1}", Uri.EscapeDataString(oauthUrl.AbsoluteUri), Uri.EscapeDataString(authSignature));
var compositeKey = string.Concat(Uri.EscapeDataString(this.ConsumerSecret), "&");
using (HMACSHA1 hasher = new HMACSHA1(Encoding.ASCII.GetBytes(compositeKey)))
{
oauthSignature = Convert.ToBase64String(hasher.ComputeHash(Encoding.ASCII.GetBytes(baseString)));
}
var authHeader = string.Format("oauth oauth_callback=\"{0}\", oauth_consumer_key=\"{1}\", oauth_nonce=\"{2}\", oauth_signature=\"{3}\", oauth_signature_method=\"{4}\", oauth_timestamp=\"{5}\", oauth_version=\"{6}\"",
Uri.EscapeDataString(callbackUrl),
Uri.EscapeDataString(this.ConsumerKey),
Uri.EscapeDataString(oauthNonce),
Uri.EscapeDataString(oauthSignature),
Uri.EscapeDataString(oauthSignatureMethod),
Uri.EscapeDataString(oauthTimestamp),
Uri.EscapeDataString(oauthVersion));
ServicePointManager.Expect100Continue = false;
HttpWebRequest authRequest = (HttpWebRequest)System.Net.WebRequest.Create(oauthUrl);
authRequest.Method = "POST";
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Headers.Add("Accept-Encoding", "gzip");
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
try
{
using (var response = authRequest.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
}
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
return sr.ReadToEnd();
}
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
我总是收到401错误。
答案 0 :(得分:0)
虽然在https://apps.twitter.com/的应用程序设置中需要回调网址,但OAuth 1.0A规范要求使用请求令牌传递回调网址。您可以将任何有效的网址传递给该请求,从而有效地让每个回调转到每个用户的唯一网址。因此,您没有锁定应用程序设置中的回调URL。