Java Code Examples for org.scribe.model.OAuthRequest#addHeader()

The following examples show how to use org.scribe.model.OAuthRequest#addHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HubicOAuth20ServiceImpl.java    From swift-explorer with Apache License 2.0 6 votes vote down vote up
public Token refreshAccessToken (Token expiredToken)
{
	if (expiredToken == null)
		return null ;
	
	OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
	
	String authenticationCode = config.getApiKey() + ":" + config.getApiSecret() ;
	byte[] bytesEncodedAuthenticationCode = Base64.encodeBase64(authenticationCode.getBytes()); 
	request.addHeader ("Authorization", "Basic " + bytesEncodedAuthenticationCode) ;
	
	String charset = "UTF-8";
	
	request.setCharset(charset);
	request.setFollowRedirects(false);
	
	AccessToken at = new HubicTokenExtractorImpl ().getAccessToken(expiredToken.getRawResponse()) ;
	
	try
	{
		request.addBodyParameter("refresh_token", at.getRefreshToken());
		//request.addBodyParameter("refresh_token", URLEncoder.encode(at.getRefreshToken(), charset));
		request.addBodyParameter("grant_type", "refresh_token");
		request.addBodyParameter(OAuthConstants.CLIENT_ID, URLEncoder.encode(config.getApiKey(), charset));
		request.addBodyParameter(OAuthConstants.CLIENT_SECRET, URLEncoder.encode(config.getApiSecret(), charset));
	} 
	catch (UnsupportedEncodingException e) 
	{			
		logger.error("Error occurred while refreshing the access token", e);
	}
	
	Response response = request.send();
	Token newToken = api.getAccessTokenExtractor().extract(response.getBody());		
	// We need to keep the initial RowResponse because it contains the refresh token
	return new Token (newToken.getToken(), newToken.getSecret(), expiredToken.getRawResponse()) ;
}
 
Example 2
Source File: XeroClient.java    From xero-java-client with Apache License 2.0 6 votes vote down vote up
protected com.connectifier.xeroclient.models.Response get(String endPoint, Date modifiedAfter, Map<String,String> params) {
  OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + endPoint);
  if (modifiedAfter != null) {
    request.addHeader("If-Modified-Since", utcFormatter.format(modifiedAfter));
  }
  if (params != null) {
    for (Map.Entry<String,String> param : params.entrySet()) {
      request.addQuerystringParameter(param.getKey(), param.getValue());
    }
  }
  service.signRequest(token, request);
  Response response = request.send();
  if (response.getCode() != 200) {
    throw newApiException(response);
  }
  return unmarshallResponse(response.getBody(), com.connectifier.xeroclient.models.Response.class);
}
 
Example 3
Source File: HubicOAuth20ServiceImpl.java    From swift-explorer with Apache License 2.0 5 votes vote down vote up
@Override
public Token getAccessToken(Token requestToken, Verifier verifier) {

	OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
	
	String authenticationCode = config.getApiKey() + ":" + config.getApiSecret() ;
	byte[] bytesEncodedAuthenticationCode = Base64.encodeBase64(authenticationCode.getBytes()); 
	request.addHeader ("Authorization", "Basic " + bytesEncodedAuthenticationCode) ;
	
	String charset = "UTF-8";
	
	request.setCharset(charset);
	request.setFollowRedirects(false);
	
	try
	{
		request.addBodyParameter(OAuthConstants.CODE, URLEncoder.encode(verifier.getValue(), charset));
		request.addBodyParameter(OAuthConstants.REDIRECT_URI, URLEncoder.encode(config.getCallback(), charset));
		request.addBodyParameter("grant_type", "authorization_code");
		request.addBodyParameter(OAuthConstants.CLIENT_ID, URLEncoder.encode(config.getApiKey(), charset));
		request.addBodyParameter(OAuthConstants.CLIENT_SECRET, URLEncoder.encode(config.getApiSecret(), charset));
	} 
	catch (UnsupportedEncodingException e) 
	{
		logger.error("Error occurred while getting the access token", e);
	}
	
	Response response = request.send();
	return api.getAccessTokenExtractor().extract(response.getBody());
}
 
Example 4
Source File: MultipartConverter.java    From jumblr with Apache License 2.0 5 votes vote down vote up
public OAuthRequest getRequest() {
    OAuthRequest request = new OAuthRequest(originalRequest.getVerb(), originalRequest.getUrl());
    request.addHeader("Authorization", originalRequest.getHeaders().get("Authorization"));
    request.addHeader("Content-Type", "multipart/form-data, boundary=" + boundary);
    request.addHeader("Content-length", bodyLength.toString());
    request.addPayload(complexPayload());
    return request;
}
 
Example 5
Source File: RequestBuilder.java    From jumblr with Apache License 2.0 5 votes vote down vote up
public OAuthRequest constructGet(String path, Map<String, ?> queryParams) {
    String url = "https://" + hostname + "/v2" + path;
    OAuthRequest request = new OAuthRequest(Verb.GET, url);
    if (queryParams != null) {
        for (Map.Entry<String, ?> entry : queryParams.entrySet()) {
            request.addQuerystringParameter(entry.getKey(), entry.getValue().toString());
        }
    }
    request.addHeader("User-Agent", "jumblr/" + this.version);

    return request;
}
 
Example 6
Source File: RequestBuilder.java    From jumblr with Apache License 2.0 5 votes vote down vote up
private OAuthRequest constructPost(String path, Map<String, ?> bodyMap) {
    String url = "https://" + hostname + "/v2" + path;
    OAuthRequest request = new OAuthRequest(Verb.POST, url);

    for (Map.Entry<String, ?> entry : bodyMap.entrySet()) {
    	String key = entry.getKey();
    	Object value = entry.getValue();
    	if (value == null || value instanceof File) { continue; }
        request.addBodyParameter(key,value.toString());
    }
    request.addHeader("User-Agent", "jumblr/" + this.version);

    return request;
}
 
Example 7
Source File: SmugMugInterface.java    From data-transfer-project with Apache License 2.0 4 votes vote down vote up
private <T> T postRequest(
    String url,
    Map<String, String> contentParams,
    @Nullable byte[] contentBytes,
    Map<String, String> smugMugHeaders,
    TypeReference<T> typeReference)
    throws IOException {

  String fullUrl = url;
  if (!fullUrl.contains("://")) {
    fullUrl = BASE_URL + url;
  }
  OAuthRequest request = new OAuthRequest(Verb.POST, fullUrl);

  // Add payload
  if (contentBytes != null) {
    request.addPayload(contentBytes);
  }

  // Add body params
  for (Entry<String, String> param : contentParams.entrySet()) {
    request.addBodyParameter(param.getKey(), param.getValue());
  }

  // sign request before adding any of the headers since those shouldn't be included in the
  // signature
  oAuthService.signRequest(accessToken, request);

  // Add headers
  for (Entry<String, String> header : smugMugHeaders.entrySet()) {
    request.addHeader(header.getKey(), header.getValue());
  }
  // add accept and content type headers so the response comes back in json and not html
  request.addHeader(HttpHeaders.ACCEPT, "application/json");

  Response response = request.send();
  if (response.getCode() < 200 || response.getCode() >= 300) {
    if (response.getCode() == 400) {
      throw new IOException(
          String.format(
              "Error occurred in request for %s, code: %s, message: %s, request: %s, bodyParams: %s, payload: %s",
              fullUrl,
              response.getCode(),
              response.getMessage(),
              request,
              request.getBodyParams(),
              request.getBodyContents()));
    }
    throw new IOException(
        String.format(
            "Error occurred in request for %s, code: %s, message: %s",
            fullUrl, response.getCode(), response.getMessage()));
  }
  return mapper.readValue(response.getBody(), typeReference);
}
 
Example 8
Source File: GoogleAPI.java    From mamute with Apache License 2.0 4 votes vote down vote up
private Response makeRequest(Token accessToken) {
	OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.googleapis.com/plus/v1/people/me");
	service.signRequest(accessToken, request);
	request.addHeader("GData-Version", "3.0");
	return request.send();
}
 
Example 9
Source File: HubicOAuth20ServiceImpl.java    From swift-explorer with Apache License 2.0 4 votes vote down vote up
@Override
public void signRequest(Token accessToken, OAuthRequest request) {
	request.addHeader("Authorization",  "Bearer " + accessToken.getToken());
}