2013-02-23 4 views
0

Google OAUTH2.0을 사용하여 웹 사이트를 방문한 사용자를 인증하고 싶습니다.방법 게시글의 WebRequest에 응답을 얻을 수 없습니다.

나는 성공적으로 응답으로 authorization_code를 얻었으며 지금은 액세스 토큰을받을 때 Google에 'POST 요청'을 할 때 문제가 발생합니다. 문제는 요청이 시간이 지남에 따라 계속되고 있으며 어떤 응답도 얻지 못하고 있다는 것입니다.

아래에 작성한 코드에 문제가 있습니까?

StringBuilder postData = new StringBuilder(); 
postData.Append("code=" + Request.QueryString["code"]); 
postData.Append("&client_id=123029216828.apps.googleusercontent.com"); 
postData.Append("&client_secret=zd5dYB9MXO4C5vgBOYRC89K4"); 
postData.Append("&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code"); 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token?code=" + Request.QueryString["code"] + "&client_id=124545459218.apps.googleusercontent.com&client_secret={zfsgdYB9MXO4C5vgBOYRC89K4}&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code"); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 
string addons = "/o/oauth2/token?code=" + Request.QueryString["code"] + "&client_id=123029216828.apps.googleusercontent.com&client_secret={zd5dYB9MXO4C5vgBOYRC89K4}&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code"; 

request.ContentLength = addons.Length; 

Stream str = request.GetResponse().GetResponseStream(); 
StreamReader r = new StreamReader(str); 
string a = r.ReadToEnd(); 

str.Close(); 
r.Close(); 
+0

당신은'postData' 문자열을 만들고 거기에 아무 것도하지 않습니다. 'addons'와'postData'가 무엇을 설명 할 수 있습니까? 이 문자열 중 하나 또는 둘 모두를 POST하려고합니까? 콘텐츠 길이 계산을위한 – JoshVarty

+0

개의 애드온 당신은 내가 webrequest.create 자체에서 모든 것을 언급했기 때문에 나는 포스트 데이터를 사용하지 않는다고 말했듯이 –

+0

그것의 작업 ... 감사합니다. 'JoshVarty'에 대한 답변입니다. –

답변

2

제 의견에 언급했듯이 코드에는 약간의 실수가 있습니다. 그것이 그대로, 당신은 실제로 아무 것도 게시하지 않습니다. postData 문자열을 보내려고합니다.

다음은 작동합니다 : 당신은 당신이 당신의 POST WebRequest 클래스에 문자열을 연결하는 부분 누락 된

//Build up your post string 
StringBuilder postData = new StringBuilder(); 
postData.Append("code=" + Request.QueryString["code"]); 
postData.Append("&client_id=123029216828.apps.googleusercontent.com"); 
postData.Append("&client_secret=zd5dYB9MXO4C5vgBOYRC89K4"); 
postData.Append("&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code"); 

//Create a POST WebRequest 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token?code=" + Request.QueryString["code"] + "&client_id=124545459218.apps.googleusercontent.com&client_secret={zfsgdYB9MXO4C5vgBOYRC89K4}&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code"); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 

//Write your post string to the body of the POST WebRequest 
var sw = new StreamWriter(request.GetRequestStream()); 
sw.Write(postData.ToString()); 
sw.Close(); 

//Get the response and read it 
var response = request.GetResponse(); 
var raw_result_as_string = (new StreamReader(response.GetResponseStream())).ReadToEnd(); 

.

+0

작업 중 .. 도움을 주셔서 감사합니다. –

관련 문제