在向Xamarin提出请求时,Http请求的内容始终为空

时间:2018-05-14 12:04:30

标签: c# azure http facebook-graph-api xamarin

我正在使用Xamarin Forms和Azure制作应用程序。我正在通过Google和Facebook进行身份验证。

在请求用户数据时,我使用HttpClient并发出请求。来自Google的请求正常,但Facebook始终返回null。

我已使用相同的访问令牌测试Postman中的Url,它会返回正确的数据。

网址是:

https://graph.facebook.com/v2.8/me/?fields=name,picture,locale,link,devices,email,first_name,last_name&access_token=

返回:

{
    "name": "Reece Russell",
    "picture": {
        "data": {
            "height": 50,
            "is_silhouette": false,
            "url": "https://lookaside.facebook.com/platform/profilepic/?asid=2139651619601242&height=50&width=50&ext=1526558014&hash=AeSKoNrLS6O4UmMV",
            "width": 50
        }
    },
    "locale": "en_US",
    "link":  "https://www.facebook.com/app_scoped_user_id/YXNpZADpBWEhTLXBnRko4LWlBRUVzc0RBZAXhsc3R1eXpNNGZAxNmJMaXhWT013WTFCMVNnOFpLNE1jblh3dWJjLUwyRDhLQ0QyOHh6NmxnVGNyX2REMU9vUzNYTHFKNjlfN0J5R0lVbkV5ZA1V4aDRBZAVNDMnpwS0EZD/",
    "devices": [
        {
            "hardware": "iPhone",
            "os": "iOS"
        }
    ],
    "first_name": "Reece",
    "last_name": "Russell",
    "id": "2139651619601242"
}

但是当使用HttpClient从Xamarin Forms发出此请求时,它总是返回null。

我正在使用的代码如下。我也尝试了很多方法来提出请求,包括从我的服务器发出请求,从FaceBook获取正确的数据,但是当我从服务器请求它仍然返回null时返回null。

using (var client = new HttpClient(new NativeMessageHandler()))
{
    string url = "https://graph.facebook.com/v2.8/me/?fields=name,picture,locale,link,devices,email,first_name,last_name&access_token=" + Settings.AccessToken;
    string json = await client.GetStringAsync(url);
    FacebookUserProfile profile = JsonConvert.DeserializeObject<FacebookUserProfile>(json);
    profile.PhotoUrl = profile.Picture.Data.Url;
    profile.Name = profile.FirstName + " " + profile.LastName;
    return profile;
}

0 个答案:

没有答案