【问题标题】:.NET MVC - QuickBooks OAuth2 API - How to set endpoints.NET MVC - QuickBooks OAuth2 API - 如何设置端点
【发布时间】:2019-06-03 16:32:11
【问题描述】:

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

如果我为代码请求添加基本 URL,QuickBooks 会正确返回带有代码的重定向 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 });
        }
    }

【问题讨论】:

    标签: .net asp.net-mvc oauth-2.0 quickbooks


    【解决方案1】:

    最后:

    从 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 });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-19
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 2016-03-01
      • 2015-10-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      相关资源
      最近更新 更多