2014-11-05 30 views
1

我想將QuickBooks API集成到我們的android應用程序中。找不到適用於Android應用程序的com.intuit.utils.OauthHelper類

下面的鏈接是爲Java Web應用程序,但我想在我的Android應用程序中的這個OauthHelper類。

https://github.com/IntuitDeveloperRelations/QuickbooksV3API-Java/blob/master/QuickbooksV3API/src/main/java/com/intuit/utils/OauthHelper.java

我也看到忒的開發者網站,但它並沒有幫助,

https://intuitpartnerplatform.lc.intuit.com/questions/825445-can-t-find-com-intuit-ia-connection-oauthhelper-class

是有一種新的方式做,或包含OauthHelper類中的任何最新的jar文件?

回答

0

JAR文件

你要添加這個罐子IPP-V3的Java的devkit-2.3.2-JAR-與依賴性,你可以從這個link下載。

OauthHelper類

public class OauthHelper { 

    public static String REQUEST_TOKEN_URL; 
    public static String ACCESS_TOKEN_URL; 
    public static String AUTHORIZE_URL; 

    public OauthHelper() { 

     REQUEST_TOKEN_URL = Constants.OAUTH_URL + "/oauth/v1/get_request_token"; 
     ACCESS_TOKEN_URL = Constants.OAUTH_URL + "/oauth/v1/get_access_token"; 
     AUTHORIZE_URL = Constants.APPCENTER_URL + "/Connect/Begin"; 
    } 

    public void getDynamicConsumer() { 

     try { 

      final String apptoken = Constants.APP_TOKEN; 

      final URL url = new URL(Constants.OAUTH_URL 
        + "/oauth/v1/create_consumer?appToken=" + apptoken); 
      final HttpURLConnection httpconnection = (HttpURLConnection) url 
        .openConnection(); 
      httpconnection.connect(); 

      StringBuffer responseBody = null; 
      int read = 0; 
      final byte buffer[] = new byte[8192]; 
      String consumerret = ""; 
      String consumerkeytoken = ""; 
      String consumerkeysecret = ""; 

      try { 
       final InputStream responseBodyStream = httpconnection 
         .getInputStream(); 
       responseBody = new StringBuffer(); 

       while ((read = responseBodyStream.read(buffer)) != -1) { 
        responseBody.append(new String(buffer, 0, read)); 
       } 
       responseBodyStream.close(); 
       consumerret = responseBody.toString(); 

       final String[] consumerkey = consumerret.split("&"); 

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

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

      } catch (Exception ex1) { 

       final int httpRespCode = httpconnection.getResponseCode(); 
       try { 
        final InputStream es = httpconnection.getErrorStream(); 
        final StringBuffer errorBody = new StringBuffer(); 
        while ((read = es.read(buffer)) != -1) { 
         errorBody.append(new String(buffer, 0, read)); 
        } 
       } catch (Exception ex2) { 
        ex2.printStackTrace(); 
       } 
      } 

     } catch (Exception ex3) { 
      ex3.printStackTrace(); 
     } 
    } 

    public Map<String, String> getRequestTokenSignPost() { 

     String authURL = null; 

     OAuthProvider provider = createProvider(); 

     String consumerkey = Constants.CONSUMER_KEY; 
     String consumersecret = Constants.CONSUMER_SECRET; 

     String callback_url = Constants.CALLBACK_URL;// WebUtils.OAUTH_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) { 
      e.printStackTrace(); 
     } 

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

     try { 
      String signedRequestTokenUrl = ouathconsumer 
        .sign(REQUEST_TOKEN_URL); 

      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) { 
       e.printStackTrace(); 
      } 
      ouathconsumer.setTokenWithSecret(ouathconsumer.getToken(), 
        ouathconsumer.getTokenSecret()); 

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

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     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) { 
      e.printStackTrace(); 
     } 
     return authorizeURL; 
    } 

    public Map<String, String> getAccessToken(String verifierCode, 
      String requestToken, String requestTokenSecret) { 
     String consumerkey = Constants.CONSUMER_KEY; 
     String consumersecret = Constants.CONSUMER_SECRET; 
     String accessToken = ""; 
     String accessTokenSecret = ""; 

     try { 
      OAuthConsumer consumer = new DefaultOAuthConsumer(consumerkey, 
        consumersecret); 
      consumer.setTokenWithSecret(requestToken, requestTokenSecret); 

      HttpParameters additionalParams = new HttpParameters(); 
      additionalParams.put("oauth_callback", "oob"); 
      additionalParams.put("oauth_verifier", verifierCode); 
      consumer.setAdditionalParameters(additionalParams); 

      String signedURL = consumer.sign(ACCESS_TOKEN_URL); 
      URL url = new URL(signedURL); 

      HttpURLConnection urlConnection = (HttpURLConnection) url 
        .openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setRequestProperty("Content-type", "application/xml"); 
      urlConnection.setRequestProperty("Content-Length", "0"); 

      String accesstokenresponse = ""; 
      if (urlConnection != null) { 
       BufferedReader rd = new BufferedReader(new InputStreamReader(
         urlConnection.getInputStream())); 
       StringBuffer sb = new StringBuffer(); 
       String line; 
       while ((line = rd.readLine()) != null) { 
        sb.append(line); 
       } 
       rd.close(); 
       accesstokenresponse = sb.toString(); 
      } 
      if (accesstokenresponse != null) { 
       String[] responseElements = accesstokenresponse.split("&"); 
       if (responseElements.length > 1) { 
        accessToken = responseElements[1].split("=")[1]; 
        accessTokenSecret = responseElements[0].split("=")[1]; 
        Map<String, String> accesstokenmap = new HashMap<String, String>(); 
        accesstokenmap.put("accessToken", accessToken); 
        accesstokenmap.put("accessTokenSecret", accessTokenSecret); 
        return accesstokenmap; 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

} 

常量類

public class Constants { 

    public static final String APP_TOKEN = ""; // First three credentials are from **Production** part not from **Development** part 
    public static final String CONSUMER_KEY = ""; 
    public static final String CONSUMER_SECRET = ""; 

    public static final String REQUEST_TOKEN_URL = "https://oauth.intuit.com/oauth/v1/get_request_token"; 
    public static final String AUTH_URL = "https://appcenter.intuit.com/Connect/Begin"; 
    public static final String ACCESS_TOKEN_URL = "https://oauth.intuit.com/oauth/v1/get_access_token"; 

    public static final String OAUTH_CALLBACK_SCHEME = "oauthflow-quickbooks"; 
    public static final String OAUTH_CALLBACK_HOST = "callback"; 

    public static final String CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" 
      + OAUTH_CALLBACK_HOST; 
    public static final String PREFERENCE_NAME = "quickbooks"; 

    public static String OAUTH_URL = "https://oauth.intuit.com"; 
    public static String APPCENTER_URL = "https://appcenter.intuit.com"; 

} 

清單文件

添加登錄啓動時以下意圖過濾器的活動。

<intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 

      <data 
       android:host="callback" 
       android:scheme="oauthflow-quickbooks" /> 
    </intent-filter> 
+0

感謝他的工作。 – exception 2014-11-05 09:34:11

+0

這個答案仍然有效嗎?我想嘗試一樣,但現在給出的鏈接無效 – 2015-11-14 06:23:02

相關問題