1

我正在尝试使用 HttpWebRequest/HttpWebResponse复制通常通过“连接到 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 验证码,而回调 URL 又需要获取访问令牌。

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

对此有什么帮助吗?谢谢!

4

1 回答 1

2

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

https://github.com/IntuitDeveloperRelations/IPP_Sample_Code

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

于 2013-08-05T17:32:26.163 回答