2
我使用下面的C#代碼,試圖讓交換我的授權代碼的訪問代碼訪問代碼交換授權代碼時:錯誤嘗試使用谷歌API
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。他們對我的申請是正確的。有人可以請指點我正確的方向嗎?