Basquiat
Basquiat

Reputation: 119

Azure Mobile Service with Facebook Auth: Get user info

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?

Upvotes: 0

Views: 1574

Answers (1)

Basquiat
Basquiat

Reputation: 119

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;

Upvotes: 1

Related Questions