【问题标题】:How do we authenticate Quickbooks Online in a Console Application?我们如何在控制台应用程序中验证 Quickbooks Online?
【发布时间】:2019-09-26 05:49:25
【问题描述】:

我正在尝试在需要首先获取持有者令牌的控制台应用程序中调用 Quick Books Online API。下面是代码 sn-p,我在其中尝试首先获取授权代码以进行后续访问令牌调用。我收到的是 HTML 响应,而不是带有验证码的 json 对象。

另外,QBO 支持哪些资助类型?

HttpClientHandler httpClientHandler = new HttpClientHandler();            
HttpClient httpClient = new HttpClient(httpClientHandler,false);
httpClient.BaseAddress = new Uri("https://appcenter.intuit.com/connect/oauth2");

List<KeyValuePair<string, string>> param = new List<KeyValuePair<string, string>>();
param.Add(new KeyValuePair<string, string>("response_type","code"));
param.Add(new KeyValuePair<string, string>("client_id", "AB5********26"));
param.Add(new KeyValuePair<string, string>("scope", "com.intuit.quickbooks.accounting"));
param.Add(new KeyValuePair<string, string>("redirect_uri", "https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl"));

var resp = httpClient.PostAsync("", new FormUrlEncodedContent(param)).GetAwaiter().GetResult();
var result = resp.Content.ReadAsStringAsync().GetAwaiter().GetResult();

我发送的请求的 HTML 响应..

<!DOCTYPE html>
<html class="dj_mac is-not-mobile" data-shell-type="node">
<!-- node-shell -->

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verIfication" content="hiEXDzwqUxxMY5KZkAkeHBn6J0gy2Ne1gJdm77RkGbk">
    <meta name="msapplication-TileColor" content="#0098cd">
    <meta charset="utf-8" />
    <link rel="preload"
        href="https://plugin.intuitcdn.net/sbg-web-shell-ui/11.24.0/bower_components/document-register-element/build/document-register-element.js"
        as="script">
    <link rel="preload" href="https://plugin.intuitcdn.net/sbg-web-shell-ui/11.24.0/dojo/dojo.js" as="script">
    <link rel="preload" href="https://plugin.intuitcdn.net/sbg-web-shell-ui/11.24.0/shell/boot.js" as="script">
    <link rel="preload" href="https://plugin.intuitcdn.net/sbg-web-shell-ui/11.24.0/dojo/resources/blank.gif"
        as="image">
    <link rel="preload" href="https://plugin.intuitcdn.net/react-dom/16.9.0/umd/react-dom.production.min.js"
        as="script">
    <link rel="preload" href="https://plugin.intuitcdn.net/react/16.9.0/umd/react.production.min.js" as="script">
    <script>
        (function() {var e = document.createEvent("Event");e.initEvent("load", true, false);window.dispatchEvent(e);})();
    </script>
    <link rel="preload" href="https://plugin.intuitcdn.net/ua-parser-js/0.7.20/dist/ua-parser.min.js" as="script">
    <meta id="viewPortMetaTag" name="viewport"
        content="width=device-width, height=device-height, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0">


    <meta name="application-name" content="QuickBooks App Store">
    <meta name="apple-mobile-web-app-title" content="QuickBooks App Store">

    <title>QuickBooks App Store</title>


    <script type="text/javascript" src="https://plugin.intuitcdn.net/sbg-web-shell-ui/11.24.0/dojo/dojo.js"></script>

</head>

<script type="text/javascript"
    src="https://plugin.intuitcdn.net/sbg-web-shell-ui/11.24.0/bower_components/document-register-element/build/document-register-element.js">
</script>
</head>

<body class="en-us">


    <script type="text/javascript" nonce="bTjY6kTz1OvXs0b/7WA0RA==">
        try {

        require({cache: {}});
        require(["shell/base/loader", "shell/applications/default/config"], function(loader, config) {
            var runtime = {"isWebpack":false,"embedded":false,"ecosystem":false,"accessDeniedPages":[48],"hiddenPages":[]};
            loader.load({
                useLayers: true,
                storageSecondaryKey: "",
                storagePrimaryKey: "",
                platformPlugin: appContext.pluginsInfo && appContext.pluginsInfo.plugins ? appContext.pluginsInfo.plugins["qbo-ui-platform"] : null,
                layers: config.getLayers(runtime)
            }, config.getAppHandler(runtime));
        });

            } catch (error){
                console.error(error);

        require({cache: {}});
        require(["shell/base/loader", "shell/applications/default/config"], function(loader, config) {
            var runtime = {"isWebpack":false,"embedded":false,"ecosystem":false,"accessDeniedPages":[48],"hiddenPages":[]};
            loader.load({
                useLayers: true,
                storageSecondaryKey: "",
                storagePrimaryKey: "",
                platformPlugin: appContext.pluginsInfo && appContext.pluginsInfo.plugins ? appContext.pluginsInfo.plugins["qbo-ui-platform"] : null,
                layers: config.getLayers(runtime)
            }, config.getAppHandler(runtime));
        });

            };
    </script>
</body>
</html>

【问题讨论】:

  • 通常要获取不记名令牌,您首先使用 REST 调用身份验证 API(即使用登录名和密码调用 Web 服务),然后它会返回不记名令牌。然后在后续调用中使用它。
  • 您是否经历过并剖析了它的工作原理? developer.intuit.com/app/developer/qbo/docs/… 原样,您的问题不够详细,任何人都无法回答
  • @Nick.McDermaid 我修改了我的问题。请让我知道你的想法...
  • 请在您的问题中添加 HTML 回复
  • @Nick.McDermaid 更新了答案中的 html 响应...

标签: c# oauth-2.0 console-application quickbooks-online


【解决方案1】:
using Intuit.Ipp.Core;
using Intuit.Ipp.OAuth2PlatformClient;
using Intuit.Ipp.Security;
using System;
using System.Collections.Generic;
using System.Text;

namespace QuickBooksToken
{
    public class GetAccessTokenQbo
    {
        public static string GetAccessToken()
        {
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

            var oauth2Client = new OAuth2Client(client Id, client Secret, "https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl", environment); // environment is “sandbox” or “production”

            var previousRefreshToken = ReadRefreshTokenFromWhereItIsStored();
            var tokenResp = oauth2Client.RefreshTokenAsync(previousRefreshToken);
            tokenResp.Wait();
            var data = tokenResp.Result;

            if (!String.IsNullOrEmpty(data.Error) || String.IsNullOrEmpty(data.RefreshToken) ||
                  String.IsNullOrEmpty(data.AccessToken))
            {
                throw new Exception("Refresh token failed - " + data.Error);
            }

            // If we've got a new refresh_token store it in the file
            if (previousRefreshToken != data.RefreshToken)
            {
                Console.WriteLine("Writing new refresh token : " + data.RefreshToken);
                WriteNewRefreshTokenToWhereItIsStored(data.RefreshToken);
return data.AccessToken
            }
            return data.AccessToken;
        }

        private static string ReadRefreshTokenFromWhereItIsStored()
        {
            return "Refresh token";       //hard code your refresh token
        }

        private static string WriteNewRefreshTokenToWhereItIsStored(string refreshToken)
        {
            return refreshToken;
        }

        public static ServiceContext GetServiceContext()
        {
            string accessToken = GetAccessToken();// Code from above
            var oauthValidator = new OAuth2RequestValidator(accessToken);

            ServiceContext qboContext = new ServiceContext(realm Id, IntuitServicesType.QBO, oauthValidator);

            return qboContext;
        }
    }
}

安装此 nuget 库 IppDotNetSdkForQuickBooksApiV3(版本 14.4.4)。 在我的情况下,环境是沙箱。您可以使用 (https://developer.intuit.com/app/developer/playground) 链接获取客户端密码、客户端 ID、领域 ID 和新刷新令牌的值,以便在您的 c# 应用程序中硬编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2020-05-02
    • 1970-01-01
    相关资源
    最近更新 更多