将Web应用程序与Quickbooks集成时的Oauth授权问题

时间:2014-01-23 10:44:03

标签: java oauth quickbooks intuit-partner-platform quickbooks-online

我正在尝试将我的Web应用程序与QuickBooks集成,我实现了Connect to QuickBooks按钮,而intuit也授予了应用程序的权限。

enter image description here

但是,我遇到We encountered a problem processing your request问题。 enter image description here

可能是什么问题?我的call_back网址中也没有得到任何回复。

我使用了Intuit Sample app中的代码。

public static String REQUEST_TOKEN_URL = "https://oauth.intuit.com/oauth/v1/get_request_token";
public static String ACCESS_TOKEN_URL = "https://oauth.intuit.com/oauth/v1/get_access_token";
public static String AUTHORIZE_URL = "https://appcenter.intuit.com/Connect/Begin";
public static String OAUTH_CONSUMER_KEY = "qyprdFHGmJjBj1jDH05Jen95Tu3PyW";
public static String OAUTH_CONSUMER_SECRET = "OMFkKCPRBQKrMoyaLg9mFYTM26kpJg8LPthbNzTB";
public static String OAUTH_CALLBACK_URL = "http://office.technology.com:8081/delegate/intuit/";

public Map<String, String> getRequestTokenSignPost() {

    String authURL = null;

    OAuthProvider provider = createProvider();

    String consumerkey = OAUTH_CONSUMER_KEY;
    String consumersecret = OAUTH_CONSUMER_SECRET;

    LOG.info("Inside getRequestToken, Consumer Key and Secret: " + consumerkey + " " + consumersecret);
    String callback_url = OAUTH_CALLBACK_URL;
    LOG.info("callback URL: " + callback_url);

    OAuthConsumer ouathconsumer = new DefaultOAuthConsumer(consumerkey, consumersecret);

    try {
        HttpParameters additionalParams = new HttpParameters();
        additionalParams.put("oauth_callback", URLEncoder.encode(callback_url, "UTF-8"));
        ouathconsumer.setAdditionalParameters(additionalParams);
    } catch (UnsupportedEncodingException e) {
        LOG.error(e.getLocalizedMessage());
    }

    String requestret = "";
    String requestToken = "";
    String requestTokenSecret = "";

    try {
        String signedRequestTokenUrl = ouathconsumer.sign(REQUEST_TOKEN_URL);
        LOG.info("signedRequestTokenUrl: " + signedRequestTokenUrl);

        URL url;

        url = new URL(signedRequestTokenUrl);

        HttpURLConnection httpconnection = (HttpURLConnection) url.openConnection();
        httpconnection.setRequestMethod("GET");
        httpconnection.setRequestProperty("Content-type", "application/xml");
        httpconnection.setRequestProperty("Content-Length", "0");
        if (httpconnection != null) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpconnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);

            }
            rd.close();
            requestret = sb.toString();
        }
        String[] requestTokenSections = requestret.split("&");

        for (int i = 0; i < requestTokenSections.length; i++) {
            String[] currentElements = requestTokenSections[i].split("=");

            if (currentElements[0].equalsIgnoreCase("oauth_token")) {
                requestToken = currentElements[1];
            } else if (currentElements[0].equalsIgnoreCase("oauth_token_secret")) {
                requestTokenSecret = currentElements[1];
            }
        }

        Map<String, String> requesttokenmap = new HashMap<String, String>();

        try {
            authURL = provider.retrieveRequestToken(ouathconsumer, callback_url);
        } catch (OAuthNotAuthorizedException e) {
            LOG.error(e.getLocalizedMessage());
        }
        ouathconsumer.setTokenWithSecret(ouathconsumer.getToken(), ouathconsumer.getTokenSecret());

        requesttokenmap.put("requestToken", requestToken);
        requesttokenmap.put("requestTokenSecret", requestTokenSecret);
        requesttokenmap.put("authURL", authURL);
        return requesttokenmap;

    } catch (OAuthMessageSignerException e) {
        LOG.error(e.getLocalizedMessage());
    } catch (OAuthExpectationFailedException e) {
        LOG.error(e.getLocalizedMessage());
    } catch (OAuthCommunicationException e) {
        LOG.error(e.getLocalizedMessage());
    } catch (MalformedURLException e) {
        LOG.error(e.getLocalizedMessage());
    } catch (IOException e) {
        LOG.error(e.getLocalizedMessage());
    }
    LOG.info("Error: Failed to get request token.");
    return null;

}

public static OAuthProvider createProvider() {
    OAuthProvider provider =
            new DefaultOAuthProvider(OauthHelper.REQUEST_TOKEN_URL, OauthHelper.ACCESS_TOKEN_URL, OauthHelper.AUTHORIZE_URL);

    return provider;
}

public String getAuthorizeURL(String requestToken, String requestTokenSecret) {

    String authorizeURL = "";
    try {
        authorizeURL = AUTHORIZE_URL + "?oauth_token=" + requestToken;
    } catch (Exception e) {
        LOG.error(e.getLocalizedMessage());
    }
    LOG.info("Authorize URL: " + authorizeURL);
    return authorizeURL;
}

我甚至得到了Request令牌:

signedRequestTokenUrl: https://oauth.intuit.com/oauth/v1/get_request_token?oauth_signature=EHKmrR%2BV%2ByF4WRcBmpkdBeYEfuE%3D&oauth_callback=http%253Aoffice.technology.com%253A8081%252Fdelegate%252Fintuit&oauth_consumer_key=qyprdFHGaJjBj1jDH05Jen95Tu3PyW&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1390538706&oauth_nonce=-4612911034475731539
requestret: oauth_token_secret=XkXjGlS6bnFvOWYthCoew54W4ILcdMWQ3jaOMCQQ&oauth_callback_confirmed=true&oauth_token=qyprdRyUiXzU0QLLavn3L3TtdqvYts5CZyomkSk8miZDfB8Y

2 个答案:

答案 0 :(得分:2)

此:

  

public static String OAUTH_CALLBACK_URL =“http:office.technology.com:8081 / delegate / intuit /”;

不是有效的网址,而且需要。修复您的网址。

答案 1 :(得分:0)

我能够使用隐身窗口修复此问题,而不会为我的Quickbooks开发者帐户添加任何Cookie。