【问题标题】:Error when trying to exchange authorization code for access code using Google API尝试使用 Google API 将授权代码交换为访问代码时出错
【发布时间】:2013-02-05 20:41:14
【问题描述】:

我正在使用以下 C# 代码尝试将我的授权码换成访问码:

        const string TokenExchangeEndPointUrl = "https://accounts.google.com/o/oauth2/token";
        string data = string.Format(
              "code={0}&client_id={1}&client_secret={2}&redirect_url={3}&grant_type=authorization_code",
              (value), (_clientId), (_clientSecret), (RedirectUri));

        var utfenc = new UTF8Encoding();
        byte[] buffer = utfenc.GetBytes(data);
        var req = (HttpWebRequest) WebRequest.Create(TokenExchangeEndPointUrl);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = buffer.Length;

        using (Stream strm = req.GetRequestStream())
        {
            strm.Write(buffer, 0, buffer.Length);
            strm.Close();
        }
        var response = (HttpWebResponse) req.GetResponse();

最后一行是发生错误的地方。我得到的错误是(400)错误请求。它发生在最后一行。从我在网上看到的例子来看,我似乎做得对。我已经通过 Fiddler2 运行了代码,它似乎与我从 Google's OAuth Playground 得到的匹配,这使得这个问题非常奇怪。我已经三次检查了我的 ClientId 和 Client Secret。它们对我的应用程序是正确的。有人可以指出我正确的方向吗?

【问题讨论】:

    标签: c# httpwebrequest oauth-2.0 google-oauth


    【解决方案1】:

    我真的想通了。我正在输入“redirect_url”。应该是“redirect_uri”

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 2012-06-24
      • 2011-08-05
      • 2014-11-05
      • 2017-08-07
      • 1970-01-01
      • 2023-04-06
      • 2014-02-14
      相关资源
      最近更新 更多