C# 如何在控制台应用程序中验证Quickbooks Online?
我试图在控制台应用程序中调用QuickBooks Online API,在该应用程序中,我需要首先获取承载令牌。下面是代码片段,我试图首先获取后续访问令牌调用的授权代码。我得到的是一个HTML响应,而不是带有auth代码的json对象 另外,QBO支持什么类型的补助金C# 如何在控制台应用程序中验证Quickbooks Online?,c#,oauth-2.0,console-application,quickbooks-online,C#,Oauth 2.0,Console Application,Quickbooks Online,我试图在控制台应用程序中调用QuickBooks Online API,在该应用程序中,我需要首先获取承载令牌。下面是代码片段,我试图首先获取后续访问令牌调用的授权代码。我得到的是一个HTML响应,而不是带有auth代码的json对象 另外,QBO支持什么类型的补助金 HttpClientHandler HttpClientHandler=new-HttpClientHandler(); HttpClient HttpClient=新的HttpClient(httpClientHandler,f
HttpClientHandler HttpClientHandler=new-HttpClientHandler();
HttpClient HttpClient=新的HttpClient(httpClientHandler,false);
httpClient.BaseAddress=新Uri(“https://appcenter.intuit.com/connect/oauth2");
列表参数=新列表();
参数添加(新的KeyValuePair(“响应类型”、“代码”);
参数Add(新的KeyValuePair(“客户机id”,“AB5**********26”);
param.Add(新的KeyValuePair(“scope”、“com.intuit.quickbooks.accounting”);
参数添加(新的KeyValuePair(“重定向uri”)https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl"));
var resp=httpClient.PostAsync(“),新表单urlencodedcontent(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>
(function(){var e=document.createEvent(“Event”);e.initEvent(“load”,true,false);window.dispatchEvent(e);})();
QuickBooks应用商店
试一试{
需要({cache:{}});
require([“shell/base/loader”,“shell/applications/default/config”],函数(loader,config){
var runtime={“isWebpack”:false,“嵌入式”:false,“生态系统”:false,“accessDeniedPages”:[48],“hiddenPages”:[]};
装载机({
是的,
storageSecondaryKey:“”,
storagePrimaryKey:“”,
platformPlugin:appContext.PluginInfo&&appContext.PluginInfo.plugins?appContext.PluginInfo.plugins[“qbo ui平台”]:null,
图层:config.getLayers(运行时)
},config.getAppHandler(运行时));
});
}捕获(错误){
控制台错误(error);
需要({cache:{}});
require([“shell/base/loader”,“shell/applications/default/config”],函数(loader,config){
var runtime={“isWebpack”:false,“嵌入式”:false,“生态系统”:false,“accessDeniedPages”:[48],“hiddenPages”:[]};
装载机({
是的,
storageSecondaryKey:“”,
storagePrimaryKey:“”,
platformPlugin:appContext.PluginInfo&&appContext.PluginInfo.plugins?appContext.PluginInfo.plugins[“qbo ui平台”]:null,
图层:config.getLayers(运行时)
},config.getAppHandler(运行时));
});
};
安装此nuget库IppDotNetSdkForQuickBooksApiV3(版本14.4.4)。
在我的例子中,环境是沙箱。您可以使用()链接获取客户端机密、客户端Id、领域Id和新刷新令牌的值,以在c#应用程序中硬编码。通常要获取承载令牌,您首先使用REST调用身份验证API(即使用登录名和密码调用web服务),然后它返回承载令牌。然后在以后的调用中使用它。您是否已经讨论过这个问题并分析了它是如何工作的?照目前的情况,你的问题不够详细,任何人都无法回答answer@Nick.McDermaid我已经修改了我的问题。请让我知道您的想法…请将HTML响应添加到您的question@Nick.McDermaid更新了答案中的html响应。。。
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;
}
}
}