谷歌开放式验证2.0增益ACCESS_TOKEN在服务器端(错误请求错误,ProtocolError) [英] Google Open Auth 2.0 gain access_token on server side (Bad Request error, ProtocolError)
问题描述
我想通过谷歌开放式验证2.0实现用户身份验证。我已经成功地从谷歌获得code和我现在试图获得的access_token接入用户的信息。这是控制器的code:
公共无效谷歌(字符串code)
{
如果(!string.IsNullOrWhiteSpace(code)条)
{
VAR参数=新词典与LT;字符串,字符串>();
参数[code] = code;
参数[的client_id] = ConfigurationProvider.GoogleApplicationId;
参数[client_secret] = ConfigurationProvider.GoogleClientSecret;
参数[REDIRECT_URI] =HTTP://本地主机:1291+ Url.Action(GoogleAuth);
参数[grant_type] =authorization_ code; VAR keyValuePairs =新的字符串[parameters.Count]
变种I = 0;
常量字符串keyValueTemplate ={0} = {1}; 的foreach(在参数变量参数)
{
keyValuePairs [I] =的String.Format(keyValueTemplate,parameter.Key,parameter.Value);
我++;
} 变种parametersString =的string.join(与&,keyValuePairs);
// $c$c=$c$c&client_id=MY.apps.googleusercontent.com&client_secret=SECRET&redirect_uri=http://localhost:1291/Account/GoogleAuth&grant_type=authorization_$c$c 常量字符串URI =https://accounts.google.com/o/oauth2/token; VAR的WebClient =新的WebClient();
webClient.Headers [Htt的prequestHeader.ContentType] =应用/的X WWW的形式urlen codeD;
webClient.UploadString(URI,POST,parametersString); //这里我得到错误的请求例外。
}
}
另外,异常包含状态,等于ProtocolError。我有同样的错误,即使我努力使POST空parametersString。
我将AP preciate的任何意见或建议。
感谢
编辑:
我也试过这个片段既没有参数(有相同的错误):
VAR数据=新的NameValueCollection
{
{code,code},
{CLIENT_ID,ConfigurationProvider.GoogleApplicationId},
{client_secret,ConfigurationProvider.GoogleClientSecret},
{REDIRECT_URI,HTTP://本地主机:1291+ Url.Action(GoogleAuth)},
{grant_type,authorization_ code}
}; VAR的WebClient =新的WebClient();
webClient.Headers [Htt的prequestHeader.ContentType] =应用/的X WWW的形式urlen codeD; //带和不带这个头?
VAR的结果= webClient.UploadValues(URI,POST,数据);
您REDIRECT_URI,必须正确转义,否则,你得到一个错误的URL。
参数[REDIRECT_URI] = Uri.EscapeDataString(_redirectUri);
I'm trying to implement user Authentication via Google Open Auth 2.0. I've already succeed to gain "code" from Google and now I'm trying to gain access_token to access users' info. This is code of the controller:
public void Google(string code)
{
if (!string.IsNullOrWhiteSpace(code))
{
var parameters = new Dictionary<string, string>();
parameters["code"] = code;
parameters["client_id"] = ConfigurationProvider.GoogleApplicationId;
parameters["client_secret"] = ConfigurationProvider.GoogleClientSecret;
parameters["redirect_uri"] = "http://localhost:1291" + Url.Action("GoogleAuth");
parameters["grant_type"] = "authorization_code";
var keyValuePairs = new string[parameters.Count];
var i = 0;
const string keyValueTemplate = "{0}={1}";
foreach (var parameter in parameters)
{
keyValuePairs[i] = string.Format(keyValueTemplate, parameter.Key, parameter.Value);
i++;
}
var parametersString = string.Join("&", keyValuePairs);
// code=CODE&client_id=MY.apps.googleusercontent.com&client_secret=SECRET&redirect_uri=http://localhost:1291/Account/GoogleAuth&grant_type=authorization_code
const string uri = "https://accounts.google.com/o/oauth2/token";
var webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
webClient.UploadString(uri, "POST", parametersString); // Here I get Bad Request exception.
}
}
Also the exception contains Status that equals "ProtocolError". I have the same error even if I try to make POST with empty parametersString.
I'll appreciate any advice or suggestion.
Thanks
Edit: I also tried this snippet both with and without parameters(have the same error):
var data = new NameValueCollection
{
{"code", code},
{"client_id", ConfigurationProvider.GoogleApplicationId},
{"client_secret", ConfigurationProvider.GoogleClientSecret},
{"redirect_uri", "http://localhost:1291" + Url.Action("GoogleAuth")},
{"grant_type", "authorization_code"}
};
var webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; // with and without this header...
var result = webClient.UploadValues(uri, "POST", data);
Your redirect_uri has to be escaped correctly, otherwise you get a malformed URL.
parameters["redirect_uri"] = Uri.EscapeDataString(_redirectUri);
这篇关于谷歌开放式验证2.0增益ACCESS_TOKEN在服务器端(错误请求错误,ProtocolError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!