将 Oauth 传递给 Rest 服务以使用它(即继续进行身份验证) [英] Pass Oauth to a Rest service to utilise it (i.e. continue authenticating)

查看:63
本文介绍了将 Oauth 传递给 Rest 服务以使用它(即继续进行身份验证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个与Xero Api,但对于 stackoverflow 来说可能足够通用.

This is a question related to the Xero Api, but might be general enough for stackoverflow.

我正在使用 Xero .Net Wrapper Library,特别是 this 使用 PublicApplicationRunner.

I'm using the Xero .Net Wrapper Library, particularly this class which uses the PublicApplicationRunner.

缩小范围,OAuth 身份验证遵循此方法(部分注释为 1.,2.,3.code>,4.):

Narrowing things down, the OAuth authentication follows this approach (sections commented 1.,2.,3.,4.):

private const string UserAgent = "Xero.API v1.0 (Public App Testing)";
private const string ConsumerKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
private const string ConsumerSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

public static Repository CreateRepository()
{
    IOAuthSession consumerSession = new XeroApiPublicSession(
        UserAgent, 
        ConsumerKey, 
        ConsumerSecret, 
        new InMemoryTokenRepository());

    consumerSession.MessageLogger = new DebugMessageLogger();

    string callback_url = "http://xx.xx.xx.xx:41000/xxx/xxx/xero/";

    Uri uri = new Uri(callback_url);

    // 1. Get a request token
    IToken requestToken = consumerSession.GetRequestToken(uri);

    // 2. Get the user to log into Xero using the request token 
    // in the query string
    string authorisationUrl = consumerSession.GetUserAuthorizationUrl();
    Process.Start(authorisationUrl);

    // 3. Get the use to enter the authorisation code from Xero
    Console.WriteLine("Please input the code you were given in Xero:");
    var verificationCode = Console.ReadLine();

    // 4. Use the request token and verification code to get an access token
    AccessToken accessToken;

    try
    {
        accessToken = consumerSession.ExchangeRequestTokenForAccessToken(
                verificationCode.Trim()
                );
    }
    catch (OAuthException ex)
    {
        Console.WriteLine(ex.Report);
        return null;
    }

    // Wrap the authenticated consumerSession in the repository...
    return new Repository(consumerSession);
}

以上是一个控制台应用程序.我试图跳过用户必须通过控制台手动输入 verificationCode(在用户登录 Xero 后会通过浏览器显示) - 即我试图跳过4..

The above is a Console Application. I'm trying to skip the user from having to manually input the verificationCode (that would have been displayed via the browser after the user logins to Xero) via the console - i.e. I'm trying to skip to section 4..

Xero 允许有一个回调 url,它在授权时被操作.因此,不是在 3. 处等待,而是执行回调并将相关授权信息以完成(不是重新开始),将 oauthentication 发送到 WCF Rest 服务:

Xero allows to have a callback url which is actioned upon authorization. So instead of waiting at 3., a callback is actioned and the relevant authorization information to complete (not start over) the oauthentication gets sent to a WCF Rest service:

?oauth_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
&oauth_verifier=#######
&org=XXXXXXXXXXXXXXXXXXX

问题是此信息位于与初始 CreateRepository() 不同线程(甚至项目)上的服务上.

The problem is this information is on a service which is on a different thread (well, project even) from the initial CreateRepository().

将此信息传递回控制台应用程序的最佳方法是什么?(来自 WCF Rest 服务).

What would be the best approach to pass this information back to the console application? (from the WCF Rest service).

简短摘要:

将信息从 WCF Rest 服务(使用控制台应用程序作为主机/运行器)传递到控制台应用程序的最佳方法(如果可能)是什么?

What is the best approach (if possible) to pass information from a WCF Rest service (which uses a console app as a host/runner) to a Console Application?

(感谢您阅读了这么长的问题)

推荐答案

您可以将回调 url 设置为 localhost(127.0.0.1:anyport).然后你可以听localhost并从那里获取验证码.无需用户手动将验证码复制粘贴到您的控制台

You can set the callback url to localhost(127.0.0.1:anyport). Then you can listen to localhost and get the verification code from there. No need for user to manually copy paste the verification code to your console

您可以在获取请求令牌时将oauth_callback参数设置为127.0.0.1:8080.引用自此处

You can set the oauth_callback parameter to 127.0.0.1:8080 while obtaining the request token. Referred from here

这篇关于将 Oauth 传递给 Rest 服务以使用它(即继续进行身份验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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