public AccessToken authUser(
      String consumerKey, String username, String password, String consumerSecret)
      throws N0ticeException {
    log.info(
        "Attempting to auth user: "******", "
            + username
            + ", "
            + password
            + ", "
            + consumerSecret);
    OAuthRequest request = createOauthRequest(Verb.POST, apiUrl + "/user/auth");
    addBodyParameter(request, "consumerkey", consumerKey);
    addBodyParameter(request, "username", username);
    addBodyParameter(request, "password", password);

    // Manually sign this request using the consumer secret rather than the access key/access
    // secret.
    addBodyParameter(request, "oauth_signature_method", "HMAC-SHA1");
    addBodyParameter(request, "oauth_version", "1.0");
    addBodyParameter(request, "oauth_timestamp", Long.toString(DateTimeUtils.currentTimeMillis()));
    final String effectiveUrl = request.getCompleteUrl() + "?" + request.getBodyContents();
    addBodyParameter(request, "oauth_signature", sign(effectiveUrl, consumerSecret));

    final Response response = request.send();
    final String responseBody = response.getBody();
    if (response.getCode() == 200) {
      return new UserParser().parseAuthUserResponse(responseBody);
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
Esempio n. 2
0
  /**
   * Invoke an HTTP GET request on a remote host. You must close the InputStream after you are done
   * with.
   *
   * @param path The request path
   * @param parameters The parameters (collection of Parameter objects)
   * @return The Response
   */
  @Override
  public com.flickr4java.flickr.Response get(
      String path, Map<String, Object> parameters, String sharedSecret) {

    OAuthRequest request = new OAuthRequest(Verb.GET, API_HOST + path);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
      request.addQuerystringParameter(entry.getKey(), String.valueOf(entry.getValue()));
    }

    if (proxyAuth) {
      request.addHeader("Proxy-Authorization", "Basic " + getProxyCredentials());
    }

    RequestContext requestContext = RequestContext.getRequestContext();
    Auth auth = requestContext.getAuth();
    if (auth != null) {
      Token requestToken = new Token(auth.getToken(), auth.getTokenSecret());
      OAuthService service = createOAuthService(parameters, sharedSecret);
      service.signRequest(requestToken, request);
    }

    if (Flickr.debugRequest) {
      logger.debug("GET: " + request.getCompleteUrl());
    }
    setTimeouts(request);
    org.scribe.model.Response scribeResponse = request.send();

    try {

      com.flickr4java.flickr.Response response = null;
      synchronized (mutex) {
        String strXml = scribeResponse.getBody();
        if (Flickr.debugStream) {
          logger.debug(strXml);
        }
        Document document = builder.parse(new InputSource(new StringReader(strXml)));
        response = (com.flickr4java.flickr.Response) responseClass.newInstance();
        response.parse(document);
      }
      return response;
    } catch (IllegalAccessException e) {
      throw new FlickrRuntimeException(e);
    } catch (InstantiationException e) {
      throw new FlickrRuntimeException(e);
    } catch (SAXException e) {
      throw new FlickrRuntimeException(e);
    } catch (IOException e) {
      throw new FlickrRuntimeException(e);
    }
  }
 /**
  * Sends an {@link OAuthRequest} and returns the {@link Response} body.
  *
  * @param request {@link OAuthRequest} corresponding to the API request
  * @return <tt>String</tt> body of API response
  */
 private String sendRequestAndGetResponse(OAuthRequest request) {
   System.out.println("Querying " + request.getCompleteUrl() + " ...");
   this.service.signRequest(this.accessToken, request);
   Response response = request.send();
   return response.getBody();
 }
Esempio n. 4
0
  /**
   * Invoke an HTTP POST request on a remote host.
   *
   * @param path The request path
   * @param parameters The parameters (collection of Parameter objects)
   * @return The Response object
   */
  @Override
  public com.flickr4java.flickr.Response post(
      String path, Map<String, Object> parameters, String sharedSecret, boolean multipart) {

    OAuthRequest request = new OAuthRequest(Verb.POST, API_HOST + path);

    if (multipart) {
      buildMultipartRequest(parameters, request);
    } else {
      buildNormalPostRequest(parameters, request);
    }

    RequestContext requestContext = RequestContext.getRequestContext();
    Auth auth = requestContext.getAuth();
    if (auth != null) {
      Token requestToken = new Token(auth.getToken(), auth.getTokenSecret());
      OAuthService service = createOAuthService(parameters, sharedSecret);
      service.signRequest(requestToken, request);
    }

    if (multipart) {
      // Ensure all parameters (including oauth) are added to payload so signature matches
      parameters.putAll(request.getOauthParameters());
      request.addPayload(buildMultipartBody(parameters, getMultipartBoundary()));
    }

    if (proxyAuth) {
      request.addHeader("Proxy-Authorization", "Basic " + getProxyCredentials());
    }

    if (Flickr.debugRequest) {
      logger.debug("POST: " + request.getCompleteUrl());
    }

    org.scribe.model.Response scribeResponse = request.send();

    try {
      com.flickr4java.flickr.Response response = null;
      synchronized (mutex) {
        String strXml = scribeResponse.getBody();
        if (Flickr.debugStream) {
          logger.debug(strXml);
        }
        if (strXml.startsWith("oauth_problem=")) {
          throw new FlickrRuntimeException(strXml);
        }
        Document document = builder.parse(new InputSource(new StringReader(strXml)));
        response = (com.flickr4java.flickr.Response) responseClass.newInstance();
        response.parse(document);
      }
      return response;
    } catch (IllegalAccessException e) {
      throw new FlickrRuntimeException(e);
    } catch (InstantiationException e) {
      throw new FlickrRuntimeException(e);
    } catch (SAXException e) {
      throw new FlickrRuntimeException(e);
    } catch (IOException e) {
      throw new FlickrRuntimeException(e);
    }
  }