Azure Mobile Service with Facebook Auth: Get user info

我的未来我决定 提交于 2019-12-19 12:01:42

问题


I am new to using Azure Mobile Services (or any mobile dev). I've followed this tutorial to enable Facebook authentication for an Android app. http://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-register-facebook-authentication/

My Mobile Service has a .NET backend and client is Android. I've also managed to save the access token to a table;

public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
    {
        var currentUser = User as ServiceUser;
        item.UserId = currentUser.Id;
        TodoItem current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);  
    }

The value saved is in this format: Facebook:907xxxxxxxxx757

I am trying to get the user's FirstName using the Facebook SDK.

var currentUser = User as ServiceUser;
        var accessToken = "current.Id";
        var client = new FacebookClient(accessToken);
        dynamic me = client.Get("me", new { fields = "name,id" });
        string firstName = me.name;

But receive the following error on my Android Virtual Device (AVD): "{message: an error has occured}" The Eclipse logcat doesn't appear to return any errors...I guess since it is not the Android App producing the error but rather the Mobile Service.

Using the person's Facebook name (which I off course will not have) works;

var currentUser = User as ServiceUser;
        var client = new FacebookClient();
        dynamic me = client.Get("zuck" });

Why doesn't this approach work?

Or is my understanding of the Facebook token incorrect? Does the token perhaps live on the AVD and therefor the Mobile Services can't use the token?


回答1:


Seems like my suspicion about the token was correct. This seems to work..

           var serviceUser = this.User as ServiceUser;
           var identities = await serviceUser.GetIdentitiesAsync();
           var fb = identities.OfType<FacebookCredentials>().FirstOrDefault();

           var client = new FacebookClient(fb.AccessToken);
           dynamic me = client.Get("me", new { fields = "name,id" });
           string firstName = me.name;
           string lastName = me.last_name;


来源:https://stackoverflow.com/questions/25463125/azure-mobile-service-with-facebook-auth-get-user-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!