示例#1
0
文件: Auth.java 项目: kaca/rest-java
  /**
   * Retrieves the access token for a given subscriber provided the subscriber has completed the
   * authentication process through web (via the login URL).
   *
   * @param appId Given app ID by Globe Labs
   * @param appSecret Given app secret by Globe Labs
   * @param code The code sent by Globe Labs to the callback URL after subscriber has completed the
   *     web authentication process
   * @return Access token and subscriber number
   * @throws GlobeApiException
   */
  public AccessTokenResponse getAccessToken(String appId, String appSecret, String code)
      throws GlobeApiException {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("app_id", appId);
    parameters.put("app_secret", appSecret);
    parameters.put("code", code);

    try {
      String requestUri = UriBuilder.buildToString(ACCESS_URI, parameters);
      HttpResponse response = client.execute(requestUri);

      String contentType = response.getEntity().getContentType().getValue();
      String[] contentTypes = contentType.split(";");
      contentType = contentTypes[0];
      if ("application/json".equals(contentType)) {
        return new AccessTokenResponse(response);
      } else {
        return new AccessTokenResponse(
            response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
      }
    } catch (ClientProtocolException e) {
      throw new GlobeApiException(e.getMessage(), e);
    } catch (URISyntaxException e) {
      throw new GlobeApiException(e.getMessage(), e);
    } catch (IOException e) {
      throw new GlobeApiException(e.getMessage(), e);
    } catch (IllegalStateException e) {
      throw new GlobeApiException(e.getMessage(), e);
    } catch (JSONException e) {
      throw new GlobeApiException(e.getMessage(), e);
    }
  }
示例#2
0
文件: Auth.java 项目: kaca/rest-java
  /**
   * Builds a login URL from a given app ID. This URL is used for the OAuth first leg.
   *
   * @param appId Given app ID by Globe Labs
   * @return Login URL for user subscription and authentication to the app
   * @throws GlobeApiException
   */
  public String getLoginUrl(String appId) throws GlobeApiException {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("app_id", appId);

    try {
      String loginUrl = UriBuilder.buildToString(REQUEST_URI, parameters);
      return loginUrl;
    } catch (URISyntaxException e) {
      throw new GlobeApiException("Given appId is invalid. Login URL cannot be parsed.", e);
    }
  }