Tuesday, 18 February 2014

How to signup/login Parse User from authenticated Twitter oauth_token in .NET?

I don't have a Twitter account so I can't test this but it looks like your POST DTO would be this:
public class TwitterAuthRequest
{
    public AuthData authData { get; set; }
}

public class Twitter
{
    public string id { get; set; }
    public string screen_name { get; set; }
    public string consumer_key { get; set; }
    public string consumer_secret { get; set; }
    public string auth_token { get; set; }
    public string auth_token_secret { get; set; }
}

public class AuthData
{
    public Twitter twitter { get; set; }
}
With a response DTO like this:
public class TwitterAuthResponse
{
    public string username { get; set; }
    public string createdAt { get; set; }
    public string updatedAt { get; set; }
    public string objectId { get; set; }
    public string sessionToken { get; set; }
    public AuthData authData { get; set; }
}

public class Twitter
{
    public string id { get; set; }
    public string screen_name { get; set; }
    public string consumer_key { get; set; }
    public string consumer_secret { get; set; }
    public string auth_token { get; set; }
    public string auth_token_secret { get; set; }
}

public class AuthData
{
    public Twitter twitter { get; set; }
}
Don't forget to put in the headers:
("X-Parse-Application-Id", ApplicationId)
("X-Parse-REST-API-Key", ApplicationKey)
EDIT:
I have a created a draft for how you would use the DTO's:
public static class TwitterLoginProvider
{
    public static Task<ServiceResponse<TwitterAuthResponse>> Login(
        Twitter twitterInfo,
        string applicationId,
        string apiKey,
        IRestClient restClient)
    {
        var request = new TwitterAuthRequest () 
        {
            authData = new AuthData () 
            {
                twitter = twitterInfo
            }
        };

        restClient.AddHeader ("X-Parse-Application-Id", applicationId);
        restClient.AddHeader ("X-Parse-REST-API-Key", apiKey);

        return restClient.PostAsync<TwitterAuthResponse>("https://api.parse.com/1/users", request, Format.Json);
    }
}
When you get the response from Xamarin.Auth, use that info to create a Twitter object and pass it to the IRestClient. As a response from the service you will get a response with the session information.
Alternatively you could use RestSharp: http://restsharp.org/

No comments:

Post a Comment