我编写了一些简单的代码,用于使用我的Twitter开发人员密钥和秘密来阅读公共时间轴上的推文。这样我就可以在自己的网站上显示用户的最新推文。代码如下。
我现在希望在Google的Blogger上与他们的博客做类似的事情。我曾经假设有一种方法可以使用我的Google API密钥和秘密来阅读博客的公共内容,而无需用户进行身份验证。我只需要博客标题和日期,所以我可以链接到Blogger网站。但我花了24小时搜索互联网,找不到任何使用密钥和秘密获取访问令牌的例子。
使用Google API SDK我已经获得了以下代码,但无法让用户进行身份验证,无法找到获取访问令牌的方法。任何建议表示感谢 - 感觉我正撞在墙上!很高兴采取任何方法 - 我只想在我正在建立的网站上获得一些Blogger内容......
使用Twitter进行身份验证:
var oAuthConsumerKey = _key;
var oAuthConsumerSecret = _secret;
var oAuthUrl = "https://api.twitter.com/oauth2/token";
// Do the Authenticate
var authHeaderFormat = "Basic {0}";
var authHeader = string.Format(authHeaderFormat,
Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
Uri.EscapeDataString((oAuthConsumerSecret)))
));
var postBody = "grant_type=client_credentials";
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "POST";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (Stream stream = authRequest.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
stream.Write(content, 0, content.Length);
}
authRequest.Headers.Add("Accept-Encoding", "gzip");
WebResponse authResponse = authRequest.GetResponse();
// deserialize into an object
string objectText;
using (authResponse)
{
using (var reader = new StreamReader(authResponse.GetResponseStream()))
{
objectText = reader.ReadToEnd();
}
}
// objectText is JSON and contains access_token
使用Blogger进行身份验证?
private bloggerTest()
{
// Register the authenticator.
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
{
ClientIdentifier = _key,
ClientSecret = _secret
};
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
// Create the service.
var service = new BloggerService(new BaseClientService.Initializer()
{
Authenticator = auth
});
var result = service.Posts.List(_blogID).Fetch();
}
private IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
var state = new AuthorizationState(new[] { BloggerService.Scopes.BloggerReadonly.GetStringValue() });
// I am stuck here!
}
答案 0 :(得分:1)
要访问公共Feed,它比您正在做的更简单。 .Net框架中内置了一堆用于处理RSS提要的类,您可以从SyndicationFeed开始。获取Feed项(博客文章)非常简单:
XDocument feed = XDocument.Load(rssUrl); //rssUrl is the URL to the rss page that (most) blogs publish
SyndicationFeed sf = SyndicationFeed.Load(feed.CreateReader());
foreach (SyndicationItem si in sf.Items)
{
..you've now got your feed item...
}
请注意,这会为您提供Feed项,但它不会为您提供它们出现的完整网页。(尽管您会获得相应的网址)。