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

.NET MVC-QuickBooks OAuth2 API-如何设置端点,.net,asp.net-mvc,oauth-2.0,quickbooks,.net,Asp.net Mvc,Oauth 2.0,Quickbooks,我不知道在哪里或者是否需要为QuickBooks OAuth2配置API端点 如果我在代码请求的基础URL前面加上前缀,QuickBooks将正确地返回到带有代码的重定向URL。之后,我不知道如何为令牌交换添加端点 在令牌交换上,我得到一个异常: Value cannot be null. Parameter name: endpoint 最终,我不知道如何正确设置端点。任何帮助都将不胜感激 public class QuickbooksController : Controller {

我不知道在哪里或者是否需要为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 });
        }
    }
公共类QuickbooksController:控制器
{
公共静态OAuth2Client OAuth2Client=新OAuth2Client(
“修订”,
“修订”,
"https://localhost:302/QuickBooks/AccessToken/",
“沙箱”);
//获取:Quickbooks
公共操作结果索引(字符串连接、字符串消息)
{
如果(!String.IsNullOrEmpty(connect)){
//准备范围
列表范围=新列表();
范围。添加(OIDScopes.Accounting);
scopes.Add(OidcScopes.OpenId);
字符串authorizationURL=oauthClient.GetAuthorizationURL(范围);
返回重定向(“https://appcenter.intuit.com/connect/oauth2“+URL);
}
ViewBag.TokenFailed=false;
ViewBag.ConfirmMessage=msg;
返回视图(新QuickBooksViewModel(新应用程序());
}
公共异步任务Accesstoken(字符串状态、字符串代码、字符串realmId)
{
试一试{
TokenResponse TokenResponse=等待oauthClient.getBealerTokenAsync(代码);
if(tokenResponse.IsError){
返回RedirectToAction(“Index”,new{msg=“连接到QuickBooks.Response时出错:”+tokenResponse.Raw});
}
返回RedirectToAction(“Index”,new{msg=“已连接到QuickBooks.Token:”+tokenResponse.AccessToken});
}捕获(例外情况除外){
返回RedirectToAction(“Index”,new{msg=“连接到QuickBooks时出错。错误:”+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 });
    }
}
公共异步任务连接() { 试试{ DiscoveryClient DiscoveryClient=新的DiscoveryClient(“https://developer.api.intuit.com/.well-known/openid_sandbox_configuration/"); DiscoveryResponse doc=等待discoveryClient.GetAsync(); 如果(IsError文件){ 返回RedirectToAction(“Index”,new{msg=“Token Endpoint.Received error:”+doc.error}); } oauthClient.DiscoveryDoc=doc; //准备范围 列表范围=新列表(); 范围。添加(OIDScopes.Accounting); scopes.Add(OidcScopes.OpenId); //scopes.Add(OidcScopes.Email); 字符串authorizationURL=oauthClient.GetAuthorizationURL(范围); 返回重定向(URL); }捕获(例外情况除外){ 返回RedirectToAction(“Index”,new{msg=“Token Endpoint.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 });
    }
}
公共异步任务连接() { 试试{ DiscoveryClient DiscoveryClient=新的DiscoveryClient(“https://developer.api.intuit.com/.well-known/openid_sandbox_configuration/"); DiscoveryResponse doc=等待discoveryClient.GetAsync(); 如果(IsError文件){ 返回RedirectToAction(“Index”,new{msg=“Token Endpoint.Received error:”+doc.error}); } oauthClient.DiscoveryDoc=doc; //准备范围 列表范围=新列表(); 范围。添加(OIDScopes.Accounting); scopes.Add(OidcScopes.OpenId); //scopes.Add(OidcScopes.Email); 字符串authorizationURL=oauthClient.GetAuthorizationURL(范围); 返回重定向(URL); }捕获(例外情况除外){ 返回RedirectToAction(“Index”,new{msg=“Token Endpoint.Error:+ex.Message}); } }