/** {@inheritDoc} */
  public Token refreshAccessToken(Token accessToken) {
    String accessTokenEndpoint = api.getAccessTokenEndpoint();
    if (accessTokenEndpoint.contains("?grant_type=")) {
      // handle the ugly case where the grant_type parameter is already hardcoded in the constant
      // url
      accessTokenEndpoint = accessTokenEndpoint.substring(0, accessTokenEndpoint.indexOf("?"));
    }
    OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), accessTokenEndpoint);

    switch (api.getAccessTokenVerb()) {
      case POST:
        request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
        request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
        // request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
        request.addBodyParameter(OAuthConstants.GRANT_TYPE, api.getRefreshTokenParameterName());
        request.addBodyParameter(api.getRefreshTokenParameterName(), accessToken.getToken());
        break;
      case GET:
      default:
        request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
        request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
        request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
        request.addQuerystringParameter(
            OAuthConstants.GRANT_TYPE, api.getRefreshTokenParameterName());
        request.addQuerystringParameter(api.getRefreshTokenParameterName(), accessToken.getToken());
    }

    Response response = request.send();
    return api.getAccessTokenExtractor().extract(response.getBody());
  }
 /**
  * Creates and sends a request to the Search API by term and location.
  *
  * <p>See <a href="http://www.yelp.com/developers/documentation/v2/search_api">Yelp Search API
  * V2</a> for more info.
  *
  * @param term <tt>String</tt> of the search term to be queried
  * @param location <tt>String</tt> of the location
  * @return <tt>String</tt> JSON Response
  */
 public String searchForBusinessesByLocation(String term, String location) {
   OAuthRequest request = createOAuthRequest(SEARCH_PATH);
   request.addQuerystringParameter("term", term);
   request.addQuerystringParameter("location", location);
   request.addQuerystringParameter("limit", String.valueOf(SEARCH_LIMIT));
   return sendRequestAndGetResponse(request);
 }
Exemplo n.º 3
0
 /**
  * Search with term and location.
  *
  * @param term Search term
  * @param latitude Latitude
  * @param longitude Longitude
  * @return JSON string response
  */
 public String search(String term, double latitude, double longitude) {
   OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
   request.addQuerystringParameter("term", term);
   request.addQuerystringParameter("ll", latitude + "," + longitude);
   this.service.signRequest(this.accessToken, request);
   Response response = request.send();
   return response.getBody();
 }
Exemplo n.º 4
0
 /**
  * Search with term and location.
  *
  * @param term Search term
  * @param latitude Latitude
  * @param longitude Longitude
  * @return JSON string response
  */
 public String search(String term, String location) {
   OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
   request.addQuerystringParameter("term", term);
   request.addQuerystringParameter("location", location);
   this.service.signRequest(this.accessToken, request);
   Response response = request.send();
   return response.getBody();
 }
Exemplo n.º 5
0
  public String search(String location) {
    OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/business_review_search");
    request.addQuerystringParameter("location", location);
    request.addQuerystringParameter("term", "food");

    request.addQuerystringParameter("ywsid", "VM6cprFxivTjEhfQsoxKhQ");
    this.service.signRequest(this.accessToken, request);
    Response response = request.send();
    return response.getBody();
  }
 /** {@inheritDoc} */
 public Token getAccessToken(Token requestToken, Verifier verifier) {
   OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
   request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
   request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
   request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
   request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
   if (config.hasScope()) request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
   System.out.println("Access Token request is:" + request);
   Response response = request.send();
   System.out.println("Got the Access Token!");
   System.out.println("(if your curious here's the response: " + response.getBody() + " )");
   return api.getAccessTokenExtractor().extract(response.getBody());
 }
Exemplo n.º 7
0
  final <T extends ServerResponse> T doPost(CommandArguments args, Class<T> clazz, String url)
      throws FlickrException {
    try {
      OAuthRequest request = new OAuthRequest(Verb.POST, url);

      // check for proxy, use if available
      if (proxy != null) {
        request.setProxy(proxy);
      }

      for (Map.Entry<String, Object> param : args.getParameters().entrySet()) {
        if (param.getValue() instanceof String) {
          request.addQuerystringParameter(param.getKey(), (String) param.getValue());
        }
      }

      oauth.signRequest(request);

      MultipartEntity multipart = args.getBody(request.getOauthParameters());
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      multipart.writeTo(baos);

      request.addPayload(baos.toByteArray());
      request.addHeader("Content-type", multipart.getContentType().getValue());

      Response response = request.send();
      String body = response.getBody();

      return parseBody(args, clazz, body);

    } catch (IOException ex) {
      throw new UnsupportedOperationException("Error preparing multipart request", ex);
    }
  }
Exemplo n.º 8
0
  public FlickrPhoto getInfo(String photoId) {

    OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.flickr.com/services/rest/");
    request.addQuerystringParameter("method", "flickr.photos.getInfo");

    request.addQuerystringParameter("api_key", auth.getApiKey());
    request.addQuerystringParameter("user_id", auth.getUserId());
    request.addQuerystringParameter("photo_id", photoId);

    request.addQuerystringParameter("format", "json");
    request.addQuerystringParameter("nojsoncallback", "1");

    Response response = auth.get(request);

    return buildInfo(Json.createReader(new StringReader(response.getBody())));
  }
Exemplo n.º 9
0
  protected void addOAuthParameter(OAuthRequest oAuthRequest, String key, String value) {

    if (oAuthRequest.getVerb() == Verb.GET) {
      oAuthRequest.addQuerystringParameter(key, value);
    } else if (oAuthRequest.getVerb() == Verb.POST) {
      oAuthRequest.addBodyParameter(key, value);
    }
  }
Exemplo n.º 10
0
 /**
  * @param parameters
  * @param request
  */
 private void buildMultipartRequest(Map<String, Object> parameters, OAuthRequest request) {
   request.addHeader("Content-Type", "multipart/form-data; boundary=" + getMultipartBoundary());
   for (Map.Entry<String, Object> entry : parameters.entrySet()) {
     String key = entry.getKey();
     if (!key.equals("photo")) {
       request.addQuerystringParameter(key, String.valueOf(entry.getValue()));
     }
   }
 }
Exemplo n.º 11
0
  /** {@inheritDoc} */
  public Token refreshAccessToken(Token accessToken) {

    String accessTokenEndpoint = api.getAccessTokenEndpoint();
    if (accessTokenEndpoint.contains("?grant_type=")) {
      // handle the ugly case where the grant_type parameter is already hardcoded in the constant
      // url
      accessTokenEndpoint = accessTokenEndpoint.substring(0, accessTokenEndpoint.indexOf("?"));
    }
    OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), accessTokenEndpoint);
    request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
    request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
    request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
    request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, api.getRefreshTokenParameterName());
    request.addQuerystringParameter(api.getRefreshTokenParameterName(), accessToken.getToken());
    Response response = request.send();
    System.out.println("Got the Refresh Token!");
    System.out.println("(if your curious here's the response: " + response.getBody() + " )");
    return api.getAccessTokenExtractor().extract(response.getBody());
  }
Exemplo n.º 12
0
 /** {@inheritDoc} */
 public Token getAccessToken(Token requestToken, Verifier verifier) {
   OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
   switch (api.getAccessTokenVerb()) {
     case POST:
       request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
       request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
       request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
       request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
       if (config.hasScope()) {
         request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
       }
       if (config.hasGrantType()) {
         request.addBodyParameter(OAuthConstants.GRANT_TYPE, config.getGrantType());
       }
       break;
     case GET:
     default:
       request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
       request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
       request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
       request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
       if (config.hasScope()) {
         request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
       }
       if (config.hasGrantType()) {
         request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, config.getGrantType());
       }
   }
   Response response = request.send();
   return api.getAccessTokenExtractor().extract(response.getBody());
 }
Exemplo n.º 13
0
 @Override
 public SearchResults searchByName(String name, Double latitude, Double longitude)
     throws Exception {
   this.service =
       new ServiceBuilder()
           .provider(Yelp2Api.class)
           .apiKey(consumerKey)
           .apiSecret(consumerSecret)
           .build();
   this.accessToken = new Token(token, tokenSecret);
   OAuthRequest request = new OAuthRequest(Verb.GET, apiUrlTemplate);
   request.addQuerystringParameter("term", name);
   request.addQuerystringParameter("ll", latitude + "," + longitude);
   this.service.signRequest(this.accessToken, request);
   Response response = request.send();
   SearchResults sr = parse(response);
   sr.setQuery(name);
   sr.setLatitude(latitude);
   sr.setLongitude(latitude);
   sr.setProvider(getName());
   return sr;
 }
Exemplo n.º 14
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);
    }
  }
Exemplo n.º 15
0
 /** {@inheritDoc} */
 public Token getAccessToken(Token requestToken, Verifier verifier) {
   OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
   request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);
   request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
   request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
   request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
   request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
   if (config.hasScope()) request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
   Response response = request.send();
   return api.getAccessTokenExtractor().extract(response.getBody());
 }
Exemplo n.º 16
0
  final <T extends ServerResponse> T doGet(CommandArguments args, Class<T> clazz)
      throws FlickrException {
    OAuthRequest request = new OAuthRequest(Verb.GET, URL_PREFIX);

    // check for proxy, use if available
    if (proxy != null) {
      request.setProxy(proxy);
    }

    for (Map.Entry<String, Object> param : args.getParameters().entrySet()) {
      request.addQuerystringParameter(param.getKey(), String.valueOf(param.getValue()));
    }

    oauth.signRequest(request);
    Response response = request.send();
    String body = response.getBody();

    return parseBody(args, clazz, body);
  }
    /**
     * {@inheritDoc}
     *
     * <p>LinkedIn request the additional 'grant_type' parameter be set.
     */
    public Token getAccessToken(Token requestToken, Verifier verifier) {
      OAuthRequest request =
          new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
      request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
      request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
      request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
      request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
      request.addQuerystringParameter("grant_type", "authorization_code");

      if (config.hasScope()) {
        request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
      }
      Response response = request.send();
      return api.getAccessTokenExtractor().extract(response.getBody());
    }
 /** {@inheritDoc} */
 public void signRequest(final Token accessToken, final OAuthRequest request) {
   request.addQuerystringParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getToken());
 }
 /**
  * {@inheritDoc}
  *
  * <p>LinkedIn uses the query parameter 'oauth2_access_token' rather than 'access_token'.
  */
 @Override
 public void signRequest(Token accessToken, OAuthRequest request) {
   request.addQuerystringParameter("oauth2_access_token", accessToken.getToken());
 }