2013-03-18 4 views
0

위의 oauth 인증을위한 키 토큰을 API를 통해 Google 제공 업체로부터 받았습니다.토큰 C에서 사용자 정보 가져 오기 #

우리가 내가 검색하는 사용자 정보 값에 대한 액세스 토큰과 함께 브라우저를 쳤을 때 지금 https://www.googleapis.com/auth/userinfo.profile

의 범위가

있다 XXXII-XXXXX-XXX-XXXXX-XXXXX입니다 "액세스 토큰"생각 해보자 내가 죽 위의 요청을 구문 분석 할 때
https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxxii-xxxxx-xxx-xxxxx-xxxxx; 

내가

{ 
"id": "XXXXXXXXXXXXXX", 
"name": "XXXXXXXXXXXXXXXX", 
"given_name": "XXXXXXXXXXX", 
"family_name": "X", 
"picture": "XXXXXXXXXX/photo.jpg", 
"locale": "en" 
} 

내 문제로 응답을한다 얻고있다 내가 사용하는 브라우저

코드 돌파로 우 코드 내가 응답을 받고 있지 않다는 응답

String userInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="+token; 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(userInfo); 
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
StreamReader sr = new StreamReader(resp.GetResponseStream()); 

sr.Close(); 

JObject jsonResp = JObject.Parse(userInfo); 
string info = ""; 
info += "<h3>" + jsonResp.Root["name"] + "</h3>"; 
info += "<img src='" + jsonResp.Root["picture"] + "' width='120'/><br/>"; 
info += "<br/>ID : " + jsonResp.Root["id"]; 
info += "<br/>Email : " + jsonResp.Root["email"]; 

Response.Write(info); 

내가 null 참조를 얻고있다. 값을 구문 분석하는 동안

내가 예기치 않은 문자

로 라인

JObject jsonResp = JObject.Parse(userInfo); 

에있어 오류가 발생했습니다 시간을. 1 호선, 위치 1.

스택 추적 : Newtonsoft.Json에서 Newtonsoft.Json.JsonTextReader.ReadInternal에서 Newtonsoft.Json.JsonTextReader.ParseValue (샤아 currentChar) () 에서

. Newtonsoft.Json.Linq.JObject.Load (JsonReader 리더)에서 Newtonsoft.Json.Linq.JObject.Parse (String json) at _Default.getresponse (String token) in d : \ Oauth \ WebSite3 \ Default.aspx.cs : 줄 99

여러분의 소중한 제안을 대기하고 사용자 정보를 얻을 수

+3

가 url'userInfo'가 아니라'sr' 응답입니다. – Caramiriel

+0

@Caramiriel sr이 응답을 얻고 있습니다. – GowthamanSS

+0

'HttpWebResponse resp'를 검사하여 질문에 결과를 추가 할 수 있습니까? HTTP 404 또는 500 상태를 반환 할 수 있습니다. – Caramiriel

답변

2

사용이 코드를 코멘트 :

var userInfoUrl = "https://www.googleapis.com/oauth2/v1/userinfo"; 
var hc = new HttpClient(); 
hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken); 
var response = hc.GetAsync(userInfoUrl).Result; 
dynamic userInfo = response.Content.ReadAsAsync().Result; 
return userInfo; 

dotnetopenoauth와 구글 API의 통합에 대한 좋은 기사가있다 : 당신은 구문 분석하고 http://www.dotnetopenauth.net/documentation/securityscenarios/

+0

당신은 위의 방법 – GowthamanSS

+0

에 대한 참조 dll 이름을 공유 할 수 있습니다. 그들은 모두 .net의 일부입니다 : System.Net.Http.dll – Dima

관련 문제