谷歌+ API错误禁止403

发布于 2024-12-11 12:57:56 字数 3923 浏览 0 评论 0原文

我正在使用此网址来获取 accesstoken

http://www.googleapis.com/plus/v1/people/me?access_token?client_id=*********.ps.googleusercontent.com&redirect_uri=http://localhost:49261/Default.aspx&client_secret=*******************

以下是我正在使用的步骤

在授权中获取代码

 string url = string.Format("{0}?client_id={1}&redirect_uri={2}", AUTHORIZE, this.ConsumerKey, CALLBACK_URL);
 url += "&scope=https://www.googleapis.com/auth/plus.me&response_type=code";

上面的网址返回一个代码并使用我正在使用的代码 将代码交换为访问令牌。

 http://www.googleapis.com/plus/v1/people/me?access_token?client_id=*********.ps.googleusercontent.com&redirect_uri=http://localhost:49261/Default.aspx&client_secret=******************* 

这是我获取 accesstoken 的代码,

public void AccessTokenGet(string authToken)
        {
            this.Token = authToken;
            string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri={2}&client_secret={3}&code={4}",
            ACCESS_TOKEN, this.ConsumerKey, CALLBACK_URL, this.ConsumerSecret, authToken);

            string response = WebRequest(Method.GET, accessTokenUrl, String.Empty);

            if (response.Length > 0)
            {
                //Store the returned access_token
                NameValueCollection qs = HttpUtility.ParseQueryString(response);

                if (qs["access_token"] != null)
                {
                    this.Token = qs["access_token"];
                }
            }
        }


        public string WebRequest(Method method, string url, string postData)
        {

            HttpWebRequest webRequest = null;
            StreamWriter requestWriter = null;
            string responseData = "";

            webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
            webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;
            webRequest.UserAgent = "[You user agent]";
            webRequest.Timeout = 20000;


            if (method == Method.POST)
            {
                webRequest.ContentType = "application/x-www-form-urlencoded";

                //POST the data.
                requestWriter = new StreamWriter(webRequest.GetRequestStream());

                try
                {
                    requestWriter.Write(postData);
                }
                catch
                {
                    throw;
                }

                finally
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            }

            responseData = WebResponseGet(webRequest);
            webRequest = null;
            return responseData;
        }


        public string WebResponseGet(HttpWebRequest webRequest)
        {
            StreamReader responseReader = null;
            string responseData = "";

            try
            {
                responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
                responseData = responseReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                webRequest.GetResponse().GetResponseStream().Close();
                responseReader.Close();
                responseReader = null;
            }

            return responseData;
        }

它在 WebResponseGet 方法中失败。返回 HTTP 403 禁止 编辑: 可能是我之前没说清楚。解释一下这个错误是什么 代码请求访问令牌失败

error: {
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit Exceeded. Please sign up",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit Exceeded. Please sign up"
 }
}

I am using this url to get the accesstoken

http://www.googleapis.com/plus/v1/people/me?access_token?client_id=*********.ps.googleusercontent.com&redirect_uri=http://localhost:49261/Default.aspx&client_secret=*******************

Here are the steps I'm using

Getting a code in authorization

 string url = string.Format("{0}?client_id={1}&redirect_uri={2}", AUTHORIZE, this.ConsumerKey, CALLBACK_URL);
 url += "&scope=https://www.googleapis.com/auth/plus.me&response_type=code";

The above url returns a code and using that code I am using
exchange the code for a accesstoken.

 http://www.googleapis.com/plus/v1/people/me?access_token?client_id=*********.ps.googleusercontent.com&redirect_uri=http://localhost:49261/Default.aspx&client_secret=******************* 

Here's my code to get the accesstoken

public void AccessTokenGet(string authToken)
        {
            this.Token = authToken;
            string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri={2}&client_secret={3}&code={4}",
            ACCESS_TOKEN, this.ConsumerKey, CALLBACK_URL, this.ConsumerSecret, authToken);

            string response = WebRequest(Method.GET, accessTokenUrl, String.Empty);

            if (response.Length > 0)
            {
                //Store the returned access_token
                NameValueCollection qs = HttpUtility.ParseQueryString(response);

                if (qs["access_token"] != null)
                {
                    this.Token = qs["access_token"];
                }
            }
        }


        public string WebRequest(Method method, string url, string postData)
        {

            HttpWebRequest webRequest = null;
            StreamWriter requestWriter = null;
            string responseData = "";

            webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
            webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;
            webRequest.UserAgent = "[You user agent]";
            webRequest.Timeout = 20000;


            if (method == Method.POST)
            {
                webRequest.ContentType = "application/x-www-form-urlencoded";

                //POST the data.
                requestWriter = new StreamWriter(webRequest.GetRequestStream());

                try
                {
                    requestWriter.Write(postData);
                }
                catch
                {
                    throw;
                }

                finally
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            }

            responseData = WebResponseGet(webRequest);
            webRequest = null;
            return responseData;
        }


        public string WebResponseGet(HttpWebRequest webRequest)
        {
            StreamReader responseReader = null;
            string responseData = "";

            try
            {
                responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
                responseData = responseReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                webRequest.GetResponse().GetResponseStream().Close();
                responseReader.Close();
                responseReader = null;
            }

            return responseData;
        }

It fails in the WebResponseGet method. Returns a HTTP 403 forbidden
EDIT:
probably I wasn't clear earlier. To explain the error here's what its is
The code fails on its request for access token

error: {
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit Exceeded. Please sign up",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit Exceeded. Please sign up"
 }
}

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

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

发布评论

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

评论(1

薄情伤 2024-12-18 12:57:56

http://www.googleapis.com/plus/v1/people/me是用于获取用户个人资料的端点(请参阅people.get 文档 )。仅当您获得访问令牌后才能调用此方法。

实际获取访问令牌本身的端点是 https://accounts.google.com/o/oauth2 /令牌。根据您要使用的 OAuth2 流程,传递给该端点的确切参数会略有不同,但所有这些参数都记录在 http://code.google.com/apis/accounts/docs/OAuth2.html

http://www.googleapis.com/plus/v1/people/me is the endpoint for fetching a user's profile (see docs for people.get). This is only callable after you get an access token.

The endpoint for actually getting the access token itself is https://accounts.google.com/o/oauth2/token. Depending on which OAuth2 flow you're wanting to use, the exact parameters you pass to that endpoint will differ slightly, but all of them are documented at http://code.google.com/apis/accounts/docs/OAuth2.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文