【问题标题】:Retrieving OAuth Verification Code via .NET HttpWebRequest通过 .NET HttpWebRequest 检索 OAuth 验证码
【发布时间】:2013-08-05 15:18:43
【问题描述】:

我正在尝试使用 HttpWebRequest/HttpWebResponse 复制通常通过“Connect to QuickBooks”按钮完成的 OAuth 步骤。

首先获取请求令牌并生成授权链接很容易:

private const string oauthBaseUrl = "https://oauth.intuit.com/oauth/v1";
private const string urlRequestToken = "/get_request_token";
private const string urlAccessToken = "/get_access_token";
private const string verifyUrl = "https://appcenter.intuit.com";
private const string authorizeUrl = "https://appcenter.intuit.com/Connect/Begin";

...

var consumerContext = new OAuthConsumerContext
                        {
                            ConsumerKey = System.Utilities.Cryptography.Encryption.ConvertToUnsecureString(ckSS),
                            ConsumerSecret = System.Utilities.Cryptography.Encryption.ConvertToUnsecureString(csSS),
                            SignatureMethod = SignatureMethod.HmacSha1
                        };
IOAuthSession session = new OAuthSession(consumerContext, oauthBaseUrl + urlRequestToken, authorizeUrl, oauthBaseUrl + urlAccessToken);
IToken requestToken = session.GetRequestToken();
string authorizationLink = session.GetUserAuthorizationUrlForToken(requestToken, callbackUrl);

然后我会在授权链接请求站点时抓取set-cookie字符串中生成的请求验证码:

var requestAuth = (HttpWebRequest) WebRequest.Create(authorizationLink);
requestAuth.Method = "GET";
requestAuth.ContentType = "application/x-www-form-urlencoded";
requestAuth.Accept = "text/html, application/xhtml+xml, */*";
requestAuth.Headers.Add("Accept-Encoding", "gzip, deflate");
requestAuth.Headers.Add("Accept-Language", "en-us");
requestAuth.Host = "appcenter.intuit.com";
requestAuth.KeepAlive = true;
var responseAuth = (HttpWebResponse) requestAuth.GetResponse();
Stream answerAuth = responseAuth.GetResponseStream();
var _answerAuth = new StreamReader(answerAuth);
string htmlAuth = _answerAuth.ReadToEnd();

// Need to grab the request verification code embedded in the set-cookie string
string cookies = responseAuth.Headers.Get("Set-Cookie");
int idx = cookies.IndexOf("__RequestVerificationToken", StringComparison.Ordinal);
if (idx > 0)
{
    int startIndex = cookies.IndexOf("=", idx, StringComparison.InvariantCultureIgnoreCase);
    int endIndex = cookies.IndexOf(";", startIndex + 1, StringComparison.InvariantCultureIgnoreCase);

    requestVerificationCode = cookies.Substring(startIndex + 1, endIndex - (startIndex + 1));
    postDataString += requestVerificationCode;
}

据我了解,需要请求验证码才能获取附加到回调 URL 的 postdata 中返回的 OAuth 验证码,而该验证码又是获取访问令牌所必需的。

这就是困难的开始。使用Fiddler2,我发现生成OAuth验证码的登录URL是https://appcenter.intuit.com/Account/LogOnJson。但无论我如何尝试使用 HttpWebRequest 复制 HTTP POST,我得到的只是 500 错误。我想知道是否有人有这一步的工作示例?这甚至可能吗?我希望如此,因为拉起 IE 并像宏一样执行相同的步骤的替代方案太丑陋了。

对此有任何帮助吗?谢谢!

【问题讨论】:

    标签: quickbooks intuit-partner-platform


    【解决方案1】:

    您可以下载 dotnet 示例应用程序以了解 OAUTH 流程的工作原理:

    https://github.com/IntuitDeveloperRelations/IPP_Sample_Code

    在 web.config 中设置您的应用密钥。

    【讨论】:

    • 感谢您的回复。问题在于不了解 OAuth 流程是如何工作的,但是,它在不需要浏览器的情况下复制它。我希望能够使用凭据登录并获取验证码,而无需浏览器。我正在尝试使用 HttpWebRequest/HttpWebResponse 来完成工作流程。
    • 那我得再检查一下。
    • 您需要使用浏览器和“连接到 QuickBooks”按钮。我们不支持您描述的方法。
    • 我找到了一个使用 python oauth2 和 cgi 的 python 示例。可能对您有用:github.com/simplegeo/python-oauth2。尽管您可能正在为不同的语言编写代码。
    猜你喜欢
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    相关资源
    最近更新 更多