用facebook登录后无法获得azure移动服务的响应

时间:2016-03-18 10:31:21

标签: facebook azure azure-mobile-services xamarin-forms

我从xamarin.forms客户端应用程序调用azure mobile service api获取用户登录的用户信息,但我无法获取用户信息。

这是我的代码。请帮忙。

Xamarin Forms code here:
async void OnFBLoginClicked(object sender, EventArgs e)
{
    MobileServiceUser user;
    try
    {
        // The authentication provider could also be Facebook, Twitter, or Microsoft

        user = await DependencyService.Get<IMobileClient>().LoginAsync(MobileServiceAuthenticationProvider.Facebook);
        MobileServiceClient client = new MobileServiceClient(Constants.ApplicationURL, Constants.ApplicationKey);

        try
        {
            var userInfo = await client.InvokeApiAsync("userInfo", HttpMethod.Get, null);
        }
        catch (Exception ex)
        {
        }
    }
}

.net后端azure移动服务代码:

[AuthorizeLevel(AuthorizationLevel.User)]
    public async Task<JObject> Get()
    {
        ServiceUser user = this.User as ServiceUser;
        if (user == null)
        {
            throw new InvalidOperationException("This can only be called by authenticated clients");
        }

        var identities = await user.GetIdentitiesAsync();
        var result = new JObject();
        var fb = identities.OfType<FacebookCredentials>().FirstOrDefault();
        if (fb != null)
        {
            var accessToken = fb.AccessToken;
            result.Add("facebook", await GetProviderInfo("https://graph.facebook.com/me?access_token=" + accessToken));
        }

        var ms = identities.OfType<MicrosoftAccountCredentials>().FirstOrDefault();
        if (ms != null)
        {
            var accessToken = ms.AccessToken;
            result.Add("microsoft", await GetProviderInfo("https://apis.live.net/v5.0/me/?method=GET&access_token=" + accessToken));
        }
        return result;
    }

    private async Task<JToken> GetProviderInfo(string url)
    {
        var c = new HttpClient();
        var resp = await c.GetAsync(url);
        resp.EnsureSuccessStatusCode();
        return JToken.Parse(await resp.Content.ReadAsStringAsync());
    }
}

0 个答案:

没有答案