DotNetOpenAuth 是一个了不起的包。但是,我一直在遇到一些障碍。直到现在他们中的大多数人都解决了,现在我遇到了一个问题,这占用了我很多时间。
问题: 我想使用DotNetOpenAuth在linkedin上分享一篇文章。我曾经能够分享一些帖子,但突然之间的事情现在已经破了,我再也无法发布股票了。我不记得在我的代码中做了很多改动。有人可以查看我的诊断,代码并查看我是否搞砸了什么?
诊断: 我通过我的代码和LinkedIn的Rest Console提出请求时运行小提琴手。以下是两种情况下的Fiddler输出:
通过linkedin
提供的Rest Console提出的请求的Fiddler输出POST /v1/people/~/shares HTTP/1.1
Authorization: OAuth oauth_consumer_key="{consumer_key}",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1362258084",oauth_nonce="-141812272",oauth_version="1.0",oauth_token="{access_token}",oauth_signature="HBMUfvHYJAxz%2BszxStVJ%2BhfQPEQ%3D"
Host: api.linkedin.com
Content-Length: 645
X-Target-URI: http://api.linkedin.com
Content-Type: application/xml
Connection: Keep-Alive
<?xml version="1.0" encoding="UTF-8"?>
<share>
<comment>test comment</comment>
<content>
<title>test title</title>
<submitted-url>{my_test_url}</submitted-url>
<submitted-image-url>{my_image_url}</submitted-image-url>
</content>
<visibility>
<code>anyone</code>
</visibility>
</share>
通过我的代码(使用DNOA)发出请求的Fiddler输出:
POST http://api.linkedin.com/v1/people/~/shares HTTP/1.1
Authorization: OAuth oauth_token="{access_token}",oauth_consumer_key="{my_consumer_key}",oauth_nonce="ECYFLc0l",oauth_signature_method="HMAC-SHA1",oauth_signature="FobR745YkMlmBpOqoTDr8CwGZIQ%3D",oauth_version="1.0",oauth_timestamp="1362263560"
Content-Type: application/xml
Host: api.linkedin.com
Content-Length: 510
Connection: Keep-Alive
<?xml version="1.0" encoding="UTF-8"?>
<share>
<comment>test comment</comment>
<content>
<title>test title</title>
<description>test description</description>
<submitted-url>{my_test_url}</submitted-url>
<submitted-image-url>{my_image_url}</submitted-image-url>
</content>
<visibility>
<code>anyone</code>
</visibility>
</share>
更多详情:
我知道我需要在执行oauth身份验证时传递范围,所以已经有了这个。当用户授权我的应用程序时,我也会获得适当的权限提示,其中显示“NETWORK UPDATES检索并向您发布更新到LinkedIn”
请注意我确实获得了访问令牌,并且我能够执行一些读取操作,例如使用此令牌获取用户详细信息,但是,我无法使用该令牌在LinkedIn上共享帖子。目前,该实施正在使用LinkedIn oAuth 1.0a。
这也是我的代码块:
public bool PostToLinkedIn(WebConsumer consumer, string sShareMessageXml, string accessToken = null)
{
var payload = Encoding.ASCII.GetBytes(sShareMessageXml);
AuthorizedTokenResponse token = null;
bool bIsAccessTokenSupplied = !string.IsNullOrEmpty(accessToken);
if (!bIsAccessTokenSupplied)
{
token = consumer.ProcessUserAuthorization();
}
if (token != null || bIsAccessTokenSupplied)
{
this.AccessToken = bIsAccessTokenSupplied ? accessToken : token.AccessToken;
var resourceEndpoint = new MessageReceivingEndpoint("http://api.linkedin.com/v1/people/~/shares", HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest);
HttpWebRequest request = consumer.PrepareAuthorizedRequest(resourceEndpoint, AccessToken);
request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
request.ContentLength = payload.Length;
request.ContentType = "application/xml";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(payload, 0, payload.Length);
}
var response = consumer.Channel.WebRequestHandler.GetResponse(request);
var responseData = response.GetResponseReader().ReadToEnd();
var xmlData = XDocument.Parse(responseData);
if (xmlData.Elements(XName.Get("update")).Any())
{
return true;
}
}
return false;
}
如果您需要更多详细信息,请与我们联系。任何帮助都会很棒。
我在DNOA的Google群组中也发布了相同问题的简短版本here。希望有人遇到这个问题并有解决方案。