获取 Google +使用 pos.plusones.get 进行计数无法使用 C#
我一直在尝试寻找如何提取 Google+ 的计数信息。我尝试使用这里的代码(稍作修改): http://johndyer.name/getting-counts-for-twitter-links-facebook-likesshares-and-google-1-plusones-in-c-or-php/。但是当我输出到屏幕上时,我不断收到以下错误:
Google says:[ { "error": { "code": 400, "message": "Invalid Value", "data": [ { "domain": "global", "reason": "invalid", "message": "Invalid Value" } ] }, "id": "p" } ]
下面是我的简单测试页代码。有什么想法吗?
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Google says:" + GetPlusOnes("http://www.google.com"));
}
string GetPlusOnes(string url)
{
string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";
string postData = @"[{""method"":""pos.plusones.get"",""id"":""p"",""params"":{""nolog"":true,""id"":""" + url + @""",""source"":""widget"",""userId"":""@viewer"",""groupId"":""@self""},""jsonrpc"":""2.0"",""key"":""p"",""apiVersion"":""v1""}]";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(googleApiUrl);
request.Method = "POST";
request.ContentType = "application/json-rpc";
request.ContentLength = postData.Length;
System.IO.Stream writeStream = request.GetRequestStream();
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
writeStream.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string jsonString = readStream.ReadToEnd();
readStream.Close();
responseStream.Close();
response.Close();
return jsonString;
//var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
//int count = Int32.Parse(json[0]["result"]["metadata"]["globalCounts"]["count"].ToString().Replace(".0", ""));
//return count;
}
这对于 facebook 和 twitter 来说是如此简单,为什么谷歌不能遵循同样的模式呢?为什么这么复杂?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为猜测,我假设您需要在这一行中用您的键注释掉:
所以它变得像这样:
Just as a guess, I assume you need to comment out
key
with yours, in this line:so it becomes something like:
是的,您需要注册。转到此处:https://code.google.com/apis /console/?pli=1#project:352290279458:服务
Yep, you need to register for it. Go here: https://code.google.com/apis/console/?pli=1#project:352290279458:services