I have written some simple code for using my Twitter developer key and secret to read tweets from a public timeline. This is so I can display a user's latest tweets in their own website. The code is below.
我编写了一些简单的代码,用于使用我的Twitter开发人员密钥和秘密来阅读公共时间轴上的推文。这样我就可以在自己的网站上显示用户的最新推文。代码如下。
I now wish to do something similar with their blog on Google's Blogger. I had assumed there would be a way to use my Google API key and secret to read the public content of a blog without the user needing to authenticate. I only need blog titles and dates so I can link through to the Blogger site. But I've spent 24 hours scouring the internet and cannot find any examples for getting an access token using just the key and secret.
我现在希望在Google的Blogger上与他们的博客做类似的事情。我曾经假设有一种方法可以使用我的Google API密钥和秘密来阅读博客的公共内容,而无需用户进行身份验证。我只需要博客标题和日期,以便我可以链接到Blogger网站。但是我花了24小时搜索互联网,并且找不到任何使用密钥和密钥获取访问令牌的示例。
Using the Google API SDK I have got as far as the code below but can't find a way to get the access token without getting the user to authenticate. Any advice appreciated - feel I'm banging my head against a wall! Happy to take any approach - I just want to get some Blogger content on a website I'm building...
使用Google API SDK我已经获得了以下代码,但无法找到获取访问令牌的方法而无法让用户进行身份验证。任何建议表示赞赏 - 觉得我正撞在墙上!很高兴采取任何方法 - 我只想在我正在建立的网站上获取一些Blogger内容...
Authenticate with Twitter:
使用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
Authenticate with Blogger??
使用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!
}
1 个解决方案
#1
1
For accessing a public feed it is way simpler than what you are doing. There are a bunch of classes built into the .Net framework for processing RSS feeds, you can start with SyndicationFeed. To get the feed items (blog posts) is quite simple:
对于访问公共源,它比您正在做的更简单。在.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...
}
Note that this will give you the feed items but it won't give you the full web page that they appear in. (although you will get a URL for that).
请注意,这将为您提供Feed项,但它不会为您提供它们出现的完整网页。(尽管您将获得该网址)。
#1
1
For accessing a public feed it is way simpler than what you are doing. There are a bunch of classes built into the .Net framework for processing RSS feeds, you can start with SyndicationFeed. To get the feed items (blog posts) is quite simple:
对于访问公共源,它比您正在做的更简单。在.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...
}
Note that this will give you the feed items but it won't give you the full web page that they appear in. (although you will get a URL for that).
请注意,这将为您提供Feed项,但它不会为您提供它们出现的完整网页。(尽管您将获得该网址)。