【问题标题】:google URL shortener API error (403 forbidden) [update]谷歌 URL 缩短 API 错误(403 禁止)[更新]
【发布时间】:2015-10-18 05:15:05
【问题描述】:

所以我尝试在我的应用中使用 Google URL Shortener API。这是我编写的用于进行 HTTP 调用并检索缩短的 URL 的类。

public class GoogleUrlShortnerApi
{
    //API Key from Google
    private const string key = "-----------MY_KEY-----------";

    public static string Shorten(string url)
    {
        string post = "{\"longUrl\": \"" + url + "\"}";

        string shortUrl = url;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + key);

        try {
            request.ServicePoint.Expect100Continue = false;
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = post.Length;
            request.ContentType = "application/json";
            request.Headers.Add("Cache-Control", "no-cache");

            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] postBuffer = Encoding.ASCII.GetBytes(post);
                requestStream.Write(postBuffer, 0, postBuffer.Length);
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader responseReader = new StreamReader(responseStream))
                    {
                        string json = responseReader.ReadToEnd();
                        shortUrl = Regex.Match(json, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;
                    }
                }
            }
        } catch (WebException webEx) {
            System.Diagnostics.Debug.WriteLine (webEx.Message);

            string responseText;
            using(var reader = new StreamReader(webEx.Response.GetResponseStream()))
            {
                responseText = reader.ReadToEnd();
            }
        } catch (Exception ex) {
            System.Diagnostics.Debug.WriteLine (ex.Message);
        }
        return shortUrl;
    }
}

但我不断收到“远程服务器返回错误:(403) Forbidden.”错误。

我尝试调试并在类中的第二个using 放置断点..

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

它永远不会进入 using 并捕获 WebException
谁能告诉我我在这里做错了什么?

感谢您的宝贵时间。

==========================更新 ============== ===========

这是来自WebExceptionresponseText 的值。我每天可以提出 1,000,000 个请求。为什么会出现此错误?

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "ipRefererBlocked",
    "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
    "extendedHelp": "https://console.developers.google.com"
   }
  ],
  "code": 403,
  "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
 }
}

【问题讨论】:

    标签: android api http xamarin google-url-shortener


    【解决方案1】:

    我想通了!!

    我创建的密钥是用于 Android 设备的,它一直给我这个错误。 因此,在意识到这是 IP 问题后,我创建了一个 SERVER KEY,因为没有其他密钥具有 IP 选项。我将服务器密钥放在我的应用程序中,然后 BOOM!成功了!

    【讨论】:

      【解决方案2】:

      在您的 API 密钥上配置了 per-IP 或 per-Referer 限制,并且请求与这些限制不匹配。如果应允许来自此 IP 或引用者的请求,请使用 Google Developers Console 更新您的 API 密钥配置。

      我了解到,因为您的 API 密钥设置为受 IP 限制,并且您的请求来自未注册使用该 API 密钥的 IP。请记住,来自模拟器的请求(很可能)与运行它们的机器具有不同的 IP,因为 Android 模拟器是一个单独的虚拟机。

      要么找出请求的来源 IP 并将其注册到您的 API 密钥中,要么(如果可能)将限制切换为 per-Referer 并在您的代码中进行处理。

      【讨论】:

      • 我没有向这个 API 密钥注册任何 IP。我什至不知道这是一个选择。当我创建 API 密钥时,我确实输入了密钥库的 SHA1 证书和 android 包名称。但是我刚刚创建了一个没有这些字段的新密钥,但仍然出现错误。
      • Google "ipRefererBlocked",听起来大家一致认为 Google 的 URL Shortener 服务很糟糕。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多