我第一次使用DontNetOpenAuth。找到博客Custom LinkedIn OAuth Provider后,我创建了自己的linkedin自定义客户端,并更改了以下内容以获取更多详细信息
const string UserProfileFields = ":(id,first-name,last-name,headline,location:(name),picture-url,email-address,public-profile-url,phone-numbers,date-of-birth)";
protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response)
{
const string profileRequestUrl =
"https://api.linkedin.com/v1/people/~" + UserProfileFields;
............
}
它运作正常。我试图对MicrosoftClient和twitter客户端使用相同的概念,但两者都不起作用。我的推特客户端如下
public class TwitterCustomClient : OAuthClient{
private static XDocument LoadXDocumentFromStream(Stream stream)
{
.......
}
private static readonly ServiceProviderDescription TwitterServiceDescription =
new ServiceProviderDescription
{
RequestTokenEndpoint = new MessageReceivingEndpoint(
"https://api.twitter.com/oauth/request_token",
HttpDeliveryMethods.GetRequest |
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
UserAuthorizationEndpoint = new MessageReceivingEndpoint(
"https://api.twitter.com/oauth/authorize",
HttpDeliveryMethods.GetRequest |
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
AccessTokenEndpoint = new MessageReceivingEndpoint(
"https://api.twitter.com/oauth/access_token",
HttpDeliveryMethods.GetRequest |
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
};
public TwitterCustomClient(string consumerKey, string consumerSecret) :
base("Twitter", TwitterServiceDescription, consumerKey, consumerSecret) { }
protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response)
{
const string profileRequestUrl = "https://api.twitter.com/1.1/";
string accessToken = response.AccessToken;
var profileEndpoint =
new MessageReceivingEndpoint(profileRequestUrl, HttpDeliveryMethods.GetRequest);
HttpWebRequest request =
WebWorker.PrepareAuthorizedRequest(profileEndpoint, accessToken);
try
{
using (WebResponse profileResponse = request.GetResponse())
{
using (Stream responseStream = profileResponse.GetResponseStream())
{
XDocument document = LoadXDocumentFromStream(responseStream);
string userId = document.Root.Element("id").Value;
string userName;
Dictionary<string, string> extraData = GetExtraData(accessToken, document, out userName);
return new AuthenticationResult(
isSuccessful: true,
provider: ProviderName,
providerUserId: userId,
userName: userName,
extraData: extraData);
}
}
}
catch (Exception exception)
{
return new AuthenticationResult(exception);
}
}
}
但它收到以下错误
异常详细信息:System.Net.WebException:返回远程服务器 错误:(401)未经授权。
来源错误:
第524行:public override void ExecuteResult(ControllerContext context)
第525行:{
第526行:OAuthWebSecurity.RequestAuthentication(Provider,ReturnUrl);
第527行:}
而且我也不确定如何获得额外的数据(对于linkedin来说,它是?fields =)。任何帮助将不胜感激。
更新:授权正在运行。
我猜以下代码无效
const string profileRequestUrl =
"https://api.twitter.com/1.1/statuses/show/:id";
string accessToken = response.AccessToken;
var profileEndpoint =
new MessageReceivingEndpoint(profileRequestUrl, HttpDeliveryMethods.GetRequest);
HttpWebRequest request =
WebWorker.PrepareAuthorizedRequest(profileEndpoint, accessToken);
request.Host = "api.twitter.com";
try
{
using (WebResponse profileResponse = request.GetResponse())
{
using (Stream responseStream = profileResponse.GetResponseStream())
{
非常感谢任何帮助。