将Oauth传递给Rest服务以使用它(即继续进行身份验证)

时间:2014-09-17 06:10:35

标签: c# .net rest oauth

这是一个与Xero Api相关的问题,但对于stackoverflow可能是一般性的。

我正在使用Xero .Net Wrapper Library,特别是使用thisPublicApplicationRunner类。

缩小范围,OAuth身份验证遵循此方法(评论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.部分。

Xero允许拥有一个在授权时执行的回调网址。因此,不是在3.等待,而是执行回调并完成相关的授权信息(不能重新开始),将oauthentication发送到WCF Rest服务:

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

问题是这些信息是在与最初的CreateRepository()不同的线程(甚至是项目甚至)上的服务上。

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

简短摘要:

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

(阅读这个长问题的荣誉)

1 个答案:

答案 0 :(得分:0)

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


您可以在获取请求令牌时将oauth_callback参数设置为127.0.0.1:8080。参考here