使用API阅读来自Blogger的公开帖子
本文关键字:Blogger API 使用 | 更新日期: 2023-09-27 18:18:47
我已经编写了一些简单的代码,用于使用我的Twitter开发人员密钥和秘密从公共时间轴读取tweet。这样我就可以在用户自己的网站上显示他们最新的推文。代码如下:
我现在希望在Google的Blogger上对他们的博客做类似的事情。我曾假设有一种方法可以使用我的Google API密钥和秘密来读取博客的公开内容,而不需要用户进行身份验证。我只需要博客标题和日期,这样我就可以链接到博客网站。但是我花了24小时在互联网上搜索,找不到任何只用密钥和秘密获得访问令牌的例子。
使用Google API SDK,我已经得到了下面的代码,但找不到一种方法来获得访问令牌而不让用户进行身份验证。任何建议都很感激-感觉我的头撞到了墙上!很高兴采取任何方法-我只是想在我正在建设的网站上获得一些博客内容…
Authenticate with 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
认证博客??
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!
}
对于访问公共提要,这比您所做的要简单得多。在。net框架中有很多处理RSS提要的类,你可以从SyndicationFeed开始。获取提要条目(博客文章)非常简单:
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...
}
注意,这将给你提要项目,但它不会给你完整的网页,他们出现在。(虽然你会得到一个URL)