Question

I would like to create some unit tests for inserting data to QuickBooks Online. I am having a problem with the authentication step:

        public DataServices Authenticate(IntuitServicesType intuitDataServicesType)
    {
        DataServices dataServices = null;

        string accessToken = HttpContext.Current.Session["accessToken"].ToString();
        string accessTokenSecret = HttpContext.Current.Session["accessTokenSecret"].ToString();
        string companyID = HttpContext.Current.Session["realm"].ToString();
        // now auth to IA
        OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, ConfigurationManager.AppSettings["consumerKey"].ToString(), ConfigurationManager.AppSettings["consumerSecret"].ToString());
        ServiceContext context = new ServiceContext(oauthValidator, accessToken, companyID, intuitDataServicesType);

        dataServices = new DataServices(context);

        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            HttpContext.Current.Session["DataServices"] = dataServices;
        }

        return dataServices;
    }

In my unit test project, which has no user interface, how can I obtain an access token and an access token secret? I cannot log into Intuit from that area.

Was it helpful?

Solution

[TestMethod()]

    public void AuthorizeWithHeadersTest()
    {
        string accessToken = ConfigurationManager.AppSettings["AccessTokenQBD"];
        string accessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecretQBD"];
        string consumerKey = ConfigurationManager.AppSettings["ConsumerKeyQBD"];
        string consumerKeySecret = ConfigurationManager.AppSettings["ConsumerSecretQBD"];
        string requestUri = "https://appcenter.intuit.com/Developer/Create";
        WebRequest webRequest = WebRequest.Create(requestUri);
        webRequest.Headers.Add("ContentType", "text/xml");
        OAuthRequestValidator target = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerKeySecret);
        target.Authorize(webRequest, string.Empty);
        Assert.IsTrue(webRequest.Headers.Count > 0);
    }

OTHER TIPS

I'm sharing a sample standalone java code snippet. You can try the same in .net

From appcenter, you can create an app to get consumer key, consumer secret and app token. Using apiexplorer and the above consumer key, consumer secret, you can get access tokens.

AppCenter - https://appcenter.intuit.com/

Apiexplorer - https://developer.intuit.com/apiexplorer?apiname=V2QBO

You can set all the 5 values in the standalone program(setupQBO method). It will work fine.

import java.util.ArrayList;
import java.util.List;

import com.intuit.ds.qb.PartyType;
import com.intuit.ds.qb.QBCustomer;
import com.intuit.ds.qb.QBCustomerService;
import com.intuit.ds.qb.QBInvalidContextException;
import com.intuit.ds.qb.QBObjectFactory;
import com.intuit.ds.qb.QBServiceFactory;
import com.intuit.platform.client.PlatformSessionContext;
import com.intuit.platform.client.PlatformServiceType;
import com.intuit.platform.client.security.OAuthCredentials;
import org.slf4j.Logger;

// QBO API Docs - https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/v2/0400_quickbooks_online/Customer
// JavaDocs     - http://developer-static.intuit.com/SDKDocs/QBV2Doc/ipp-java-devkit-2.0.10-SNAPSHOT-javadoc/

public class CodegenStubCustomerall {

    static String accesstoken = "";
    static String accessstokensecret = "";
    static String appToken = "";
    static String oauth_consumer_key = "";
    static String oauth_consumer_secret = "";
    static String realmID = "";
    static String dataSource = "";

    final PlatformSessionContext context;

    public CodegenStubCustomerall(PlatformSessionContext context) {
        this.context = context;
    }

    public void testAdd(){
        try {
            QBCustomer entityPojo = QBObjectFactory.getQBObject(context, QBCustomer.class);
            entityPojo.setName("TestQBCustomer12345");
            entityPojo.setTypeOf(PartyType.PERSON);

            QBCustomerService service = QBServiceFactory.getService(context, QBCustomerService.class);
            QBCustomer qbQBCustomer = service.addCustomer(context, entityPojo);

        } catch (QBInvalidContextException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<QBCustomer> testGetAll() {
        final List<QBCustomer> entityList = new ArrayList<QBCustomer>();
        try {
            QBCustomerService service = QBServiceFactory.getService(context, QBCustomerService.class);
                List<QBCustomer> qbCustomerList = service.findAll(context, 1,100);
                for (QBCustomer each : qbCustomerList) {
                    entityList.add(each);
                }
        } catch (QBInvalidContextException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entityList;
    }

    public static void main(String args[]) {
        PlatformSessionContext context = getPlatformContext("QBO");
        CodegenStubCustomerall testObj = new CodegenStubCustomerall(context);
        testObj.testGetAll();
    }

    public static PlatformSessionContext getPlatformContext(String dataSource) {

        PlatformServiceType serviceType = null;
        if (dataSource.equalsIgnoreCase("QBO")) {
            serviceType = PlatformServiceType.QBO;
            setupQBO();
        }

        final OAuthCredentials oauthcredentials = new OAuthCredentials(
                oauth_consumer_key, oauth_consumer_secret, accesstoken,
                accessstokensecret);

        final PlatformSessionContext context = new PlatformSessionContext(
                oauthcredentials, appToken, serviceType, realmID);

        return context;
    }

    private static void setupQBO() {
        System.out.println("QBO token setup");
        accesstoken = "replace your tokens";
        accessstokensecret = "replace your tokens";
        appToken = "replace your tokens";
        oauth_consumer_key = "replace your tokens";
        oauth_consumer_secret = "replace your tokens";
        realmID = "7123456720";
        dataSource = "QBO";
    }

}

For sample .net code, you can refer this link. https://developer.intuit.com/docs/0025_quickbooksapi/0055_devkits/0100_ipp_.net_devkit/0299_synchronous_calls/0001_data_service_apis

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top