如何从 .NET 中经过身份验证的 Twitter oauth_token 注册/登录解析用户? [英] How to signup/login Parse User from authenticated Twitter oauth_token in .NET?

查看:18
本文介绍了如何从 .NET 中经过身份验证的 Twitter oauth_token 注册/登录解析用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 OAuth1Authenticator 类来自 Xamarin.Auth 组件库,允许用户通过 Twitter 登录.它正确验证并给我一个响应,其中包含以下内容:oauth_token、oauth_token_secret、user_id、screen_name、oauth_consumer_key、oauth_consumer_secret.

I've used the OAuth1Authenticator class from the Xamarin.Auth component library to allow users to login via Twitter. It authenticates correctly and gets me a response which has the following: oauth_token, oauth_token_secret, user_id, screen_name, oauth_consumer_key, oauth_consumer_secret.

这是我的代码

OAuth1Authenticator twitterAuthenticator = new OAuth1Authenticator(Constants.Twitter.CONSUMER_KEY, Constants.Twitter.CONSUMER_SECRET, new Uri(Constants.Twitter.REQUEST_TOKEN_URL), new Uri(Constants.Twitter.AUTHORIZE_URL), new Uri(Constants.Twitter.ACCESS_TOKEN_URL), new Uri(Constants.Twitter.CALLBACK_URL));

            twitterAuthenticator.Completed += async (sender, e) =>
                {    
                    if (!e.IsAuthenticated)
                    {
                        return;
                    }

                    string oauth_token = e.Account.Properties["oauth_token"].ToString();

问题是我如何使用该响应来注册/登录解析用户?即我想要通过 Twitter 令牌在解析数据库上创建一个 ParseUser 并且应该处理会话,它的工作方式与 相同使用 ParseFacebookUtils 类通过 Facebook 签名

The question is how do I then use that response to signup/signin a Parse User ? i.e. I want a ParseUser created on the parse database via the Twitter token and the session should be taken care of, same way it works for sign-via-Facebook using ParseFacebookUtils class

问题是 Parse 不支持在 Xamarin 中通过 Twitter 登录,但是,我相信 parse 确实支持任何类型的 3rd 方身份验证,如下所示,但我不知道该怎么做.

Problem is Parse doesn't support Login via Twitter in Xamarin, however, I believe parse does support any type of 3rd party authentication in an alternative way as shown below but I don't know how to do it.

这里是最相关的链接

https://parse.com/tutorials/adding-third-party-authentication-to-your-web-app 但此链接中的问题是它是作为网页按钮制作的,不知道如何在移动设备上使用它,它是用于 GitHub 的,不知道如何将其用于 Twitter(Twitter 只是 OAuth1)

https://parse.com/tutorials/adding-third-party-authentication-to-your-web-app but the problem in this link is that it's made as a webpage button, don't know how to use that on a mobile, and it's for GitHub don't know how to use it for Twitter instead (Twitter is only OAuth1)

http://blog.parse.com/2013/12/03/带上你自己的登录/ 这正是我需要的,但它需要一个会话令牌,不适用于 twitter 回复我的 oauth_tokens,因此不知道如何使用提到的方法在链接中

http://blog.parse.com/2013/12/03/bring-your-own-login/ This is exactly what I need but it needs a session Token, doesn't work with the oauth_tokens that twitter responds back to me, hence don't know how to use the method mentioned in the link

https://github.com/auth0/rules/blob/master/parse.md 这看起来像解决方案,但是我不知道如何使用它,它确实显示了 twitter 图标,因此它应该与 twtiter 一起使用,但是我如何让它在 .NET 中工作 更新: 我发现这个 xamarin 组件 http://components.xamarin.com/view/Auth0Client 让我更接近使用本段第一个链接中提到的方法,但我还是迷路了,不知道如何链接autho0来解析

https://github.com/auth0/rules/blob/master/parse.md This looks like the solution, however I don't know how to use it, it does show the twitter icon so it should work with twtiter but how do I get that to work in .NET Update: I've found this xamarin component http://components.xamarin.com/view/Auth0Client which gets me closer to use the method mentioned in the first link in this paragraph, yet I'm still lost and don't know how to link autho0 to parse

总而言之,我迷失在这个迷宫中,真的希望有人能帮助我.

All in all, I'm lost in this maze and really wish anyone could help me out.

推荐答案

我没有 Twitter 帐户,所以我无法对此进行测试,但看起来您的 POST DTO 应该是这样的:

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; }
}

像这样的响应 DTO:

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; }
}

不要忘记放入标题:

("X-Parse-Application-Id", ApplicationId)
("X-Parse-REST-API-Key", ApplicationKey)

来源:https://parse.com/docs/rest#users

我为您如何使用 DTO 创建了一个草案:

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);
    }
}

当您从 Xamarin.Auth 获得响应时,使用该信息创建 Twitter 对象并将其传递给 IRestClient.作为来自服务的响应,您将获得包含会话信息的响应.

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.

IRestClient 接口:https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Web/IRestClient.cs

IRestClient interface: https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Web/IRestClient.cs

示例实现:https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Web/RestClient.cs

或者您可以使用 RestSharp:http://restsharp.org/

Alternatively you could use RestSharp: http://restsharp.org/

这篇关于如何从 .NET 中经过身份验证的 Twitter oauth_token 注册/登录解析用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆