Google URL Shortener API错误(禁止403)[更新] [英] google URL shortener API error (403 forbidden) [update]

查看:599
本文介绍了Google URL Shortener 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)禁止."错误.

But I keep getting the "The remote server returned an error: (403) Forbidden." error.

我试图调试并在类的第二个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.请记住,由于Android模拟器是一个单独的VM,因此来自模拟器的请求(很有可能)将具有与运行它们的计算机不同的IP.

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.

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

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