谷歌 URL 缩短器 API 错误(403 禁止)[更新] [英] google URL shortener API error (403 forbidden) [update]

查看:28
本文介绍了谷歌 URL 缩短器 API 错误(403 禁止)[更新]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to use Google URL Shortener API in my app. Here's the class I wrote to make the HTTP call and retrieve the shortened 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 上设置断点..

I tried to debug and put a breakpoint on the 2nd using in the class..

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

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

It never goes inside that using and catches a WebException.
Can anyone give me an idea on what I'm doing wrong here?

感谢您抽出宝贵时间.

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

========================= UPDATE =========================

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

This is the value of the responseText from the WebException. I'm allowed to make 1,000,000 request per day. Why am I getting this error?

{
 "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."
 }
}

推荐答案

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

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.

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

I read that as your API key is setup to be restricted by IP and your request is coming from an IP that is not registered to use that API key. Keep in mind that requests from the emulator will (most likely) have a different IP than the machine they are running on, because the Android emulator is a separate VM.

找出请求源自的 IP 并使用您的 API 密钥注册它,或者(如果可能)将限制切换为每个引用者并在您的代码中处理.

Either figure out the IP that the request is originating from and register it with your API key, or (if possible) switch the restriction to be per-Referer and handle that in your code.

这篇关于谷歌 URL 缩短器 API 错误(403 禁止)[更新]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆