.NET MVC - QuickBooks OAuth2 API - 如何设置端点

.NET MVC - QuickBooks OAuth2 API - How to set endpoints

我不知道在哪里或是否需要为 QuickBooks OAuth2 配置 API 端点。

如果我为代码请求添加基础 URL,QuickBooks 会使用代码正确地 returns 到重定向 URL。在那之后,我不知道如何添加令牌交换的端点。

在令牌交换中,我遇到异常:

Value cannot be null. Parameter name: endpoint

最终,我不知道如何正确设置端点。任何帮助将不胜感激。

public class QuickbooksController : Controller
{
    public static OAuth2Client oauthClient = new OAuth2Client(
        "REDACTED",
        "REDACTED",
        "https://localhost:302/QuickBooks/AccessToken/",
        "sandbox");

    // GET: Quickbooks
    public ActionResult Index(string connect, string msg)
    {
        if (!String.IsNullOrEmpty(connect)) {

            //Prepare scopes
            List<OidcScopes> scopes = new List<OidcScopes>();
            scopes.Add(OidcScopes.Accounting);
            scopes.Add(OidcScopes.OpenId);

            string authorizeUrl =  oauthClient.GetAuthorizationURL(scopes);

            return Redirect("https://appcenter.intuit.com/connect/oauth2" + authorizeUrl);
        }

        ViewBag.TokenFailed = false;
        ViewBag.ConfirmMessage = msg;
        return View(new QuickBooksViewModel(new App()));
    }

    public async Task<ActionResult> Accesstoken(string state, string code, string realmId)
    {
        try {
            TokenResponse tokenResponse = await oauthClient.GetBearerTokenAsync(code);

            if (tokenResponse.IsError) { 
                return RedirectToAction("Index", new { msg = "Error connecting to QuickBooks. Response: "  + tokenResponse.Raw });
            }

            return RedirectToAction("Index", new { msg = "Connected to QuickBooks. Token: " + tokenResponse.AccessToken });
        } catch (Exception ex) {
            return RedirectToAction("Index", new { msg = "Error connecting to QuickBooks. Error: " + ex.Message });
        }
    }

最后:

从 URL 获取发现文档并将其分配给 oauth 客户端。发现文档与生产环境不同,因此您需要使其可配置。

确保在发现文档响应后检查错误。我缺少一个没有引发错误的 dll。

在此之后,所有端点都应该可以正常工作,无需预先添加或分配任何内容。希望这能为其他人节省几个小时。

public async Task<ActionResult> Connect()
{
    try { 
        DiscoveryClient discoveryClient = new DiscoveryClient("https://developer.api.intuit.com/.well-known/openid_sandbox_configuration/");
        DiscoveryResponse doc = await discoveryClient.GetAsync();
        if (doc.IsError) {
            return RedirectToAction("Index", new { msg = "Token Endpoint. Received error: " + doc.Error });
        }

        oauthClient.DiscoveryDoc = doc;

        //Prepare scopes
        List<OidcScopes> scopes = new List<OidcScopes>();
        scopes.Add(OidcScopes.Accounting);
        scopes.Add(OidcScopes.OpenId);
        //scopes.Add(OidcScopes.Email);

        string authorizeUrl = oauthClient.GetAuthorizationURL(scopes);

        return Redirect(authorizeUrl);
    } catch(Exception ex) {
        return RedirectToAction("Index", new { msg = "Token Endpoint. Error: " + ex.Message });
    }
}