我正在使用以下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。它们对我的申请是正确的。有人可以指出我正确的方向吗?
答案 0 :(得分:4)
我实际上已经明白了。我正在放“redirect_url”。它应该是“redirect_uri”