获取 Google +使用 pos.plusones.get 进行计数无法使用 C#

发布于 2024-12-11 01:38:43 字数 2286 浏览 0 评论 0 原文

我一直在尝试寻找如何提取 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 来说是如此简单,为什么谷歌不能遵循同样的模式呢?为什么这么复杂?

I have been trying to find how to pull out the count information for Google+. I have tried using the code here (slightly modified): http://johndyer.name/getting-counts-for-twitter-links-facebook-likesshares-and-google-1-plusones-in-c-or-php/. But I keep getting the following error when I out put to screen:

Google says:[ { "error": { "code": 400, "message": "Invalid Value", "data": [ { "domain": "global", "reason": "invalid", "message": "Invalid Value" } ] }, "id": "p" } ] 

My simple test page code below. Any ideas?

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;
}

This is so simple to do with facebook and twitter, why couldn't google follow the same pattern? Why so complex?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-12-18 01:38:43

作为猜测,我假设您需要在这一行中用您的键注释掉:

string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";

所以它变得像这样:

string googleApiUrl = "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";

Just as a guess, I assume you need to comment out key with yours, in this line:

string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";

so it becomes something like:

string googleApiUrl = "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文