1

上記のoauth認証のキートークンをGoogleプロバイダーからAPIを介して取得しました。

「アクセストークン」が xxxii-xxxxx-xxx-xxxxx-xxxxx であり、スコープがhttps://www.googleapis.com/auth/userinfo.profileであると考えてみましょう

ユーザー情報の値を取得するためのアクセストークンでブラウザーをヒットすると、

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);

なので

値の解析中に予期しない文字が検出されました: h。行 1、位置 1。

スタックトレース:

Newtonsoft.Json.JsonTextReader.ParseValue(Char currentChar) で Newtonsoft.Json.JsonTextReader.ReadInternal() で Newtonsoft.Json.JsonTextReader.Read() で 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:line 99

貴重なご意見・ご感想お待ちしております

4

1 に答える 1

3

次のコードを使用して、ユーザー情報を取得します。

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 と google api の統合に関する良い記事があります: http://www.dotnetopenauth.net/documentation/securityscenarios/

于 2013-03-18T07:38:24.473 に答える