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 を起動して、マクロのように同じ手順を実行するという代替手段はあまりにも醜いので、そう願っています。
これについて何か助けはありますか?ありがとう!