/** * Sets the {@code Authorization} header. * * @param authorization the authorization * @return this */ public Builder setAuthorization(@Nullable Authorization authorization) { if (authorization != null) { headers.setAuthorization(authorization.toString()); } return this; }
private String requestRaw(String path) throws IOException { HttpRequest getRequest = TRANSPORT.createRequestFactory().buildGetRequest( new GenericUrl(baseUrl + path)); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("Bearer " + accessToken); getRequest.setHeaders(headers); HttpResponse response = getRequest.execute(); validateResponse(getRequest, response, 200); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(response.getContent(), byteArrayOutputStream, true); return byteArrayOutputStream.toString(); }
/** Posts a new status for the user, initially marked as private.**/ public void postStatus(String content, String idempotencyKey) throws IOException { ImmutableMap<String, String> formParams = ImmutableMap.of( "status", content, // Default everything to private to avoid a privacy incident "visibility", "private" ); UrlEncodedContent urlEncodedContent = new UrlEncodedContent(formParams); HttpRequest postRequest = TRANSPORT.createRequestFactory() .buildPostRequest( new GenericUrl(baseUrl + POST_URL), urlEncodedContent) .setThrowExceptionOnExecuteError(false); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("Bearer " + accessToken); if (!Strings.isNullOrEmpty(idempotencyKey)) { // This prevents the same post from being posted twice in the case of network errors headers.set("Idempotency-Key", idempotencyKey); } postRequest.setHeaders(headers); HttpResponse response = postRequest.execute(); validateResponse(postRequest, response, 200); }
HttpHeaders httpHeaders = request.getHeaders().clone().setAuthorization("Bearer " + idToken);
/** * Sets the {@code Authorization} header. * * @param authorization the authorization * @return this */ public Builder setAuthorization(@Nullable Authorization authorization) { if (authorization != null) { headers.setAuthorization(authorization.toString()); } return this; }
/** * Sets the {@code Authorization} header. * * @param authorization the authorization * @return this */ public Builder setAuthorization(@Nullable Authorization authorization) { if (authorization != null) { headers.setAuthorization(authorization.toString()); } return this; }
/** * Sets the {@code "Authorization"} header to this request. * * @param token the authorization token to be set, {@code null} value will be ignored */ public void authorization( @Nullable String token ) { boolean hasAuthScheme = false; if ( !Strings.isNullOrEmpty( token ) ) { for ( AuthScheme scheme : AuthScheme.values() ) { if ( token.startsWith( scheme.value ) ) { headers.setAuthorization( token ); hasAuthScheme = true; break; } } if ( !hasAuthScheme ) { String finalToken = authScheme == null ? token : authScheme.value + " " + token; headers.setAuthorization( finalToken ); } } }
@EBean(scope = Scope.Singleton) public class MyAuthInterceptor implements ClientHttpRequestInterceptor { @Bean MyAuthStore authStore; public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); HttpAuthentication auth = new HttpBasicAuthentication(authStore.getUsername(), authStore.getPassword()); headers.setAuthorization(auth); return execution.execute(request, body); } }
@Override public Void answer(InvocationOnMock unused) { req.getHeaders().setAuthorization(authHeaderValue); return null; } })
@Override public Void answer(InvocationOnMock unused) { req.getHeaders().setAuthorization(authHeaderValue); return null; } })
@Override public Void answer(InvocationOnMock unused) { req.getHeaders().setAuthorization(authHeaderValue); return null; } })
@Override public Void answer(InvocationOnMock unused) { req.getHeaders().setAuthorization(authHeaderValue); return null; } })
@Override public Void answer(InvocationOnMock unused) { req.getHeaders().setAuthorization(authHeaderValue); return null; } }).when(mockCredential).intercept(eq(req));
@Override public Void answer(InvocationOnMock unused) { req.getHeaders().setAuthorization(authHeaderValue); return null; } })
@Override public Void answer(InvocationOnMock unused) { req.getHeaders().setAuthorization(authHeaderValue); return null; } })
public void intercept(HttpRequest request) throws IOException { computeNonce(); computeTimestamp(); try { computeSignature(request.getRequestMethod(), request.getUrl()); } catch (GeneralSecurityException e) { logger.error(e); IOException io = new IOException(); io.initCause(e); throw io; } request.getHeaders().setAuthorization(getAuthorizationHeader()); }
private JsonObject graphql(String query) throws IOException { String payload = Json.createObjectBuilder() .add("query", query) .add("variables", "{}") .build().toString(); HttpRequest request = requestFactory.buildPostRequest( new GenericUrl(GRAPHQL_ENDPOINT), ByteArrayContent.fromString("application/json", payload)); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("bearer " + tokenFactory.getToken()); request.setHeaders(headers); HttpResponse response = executeWithRetry(request); // TODO: Handle error status code JsonObject responseObject = Json.createReader(new StringReader(response.parseAsString())).readObject(); if (responseObject.containsKey("errors")) { LOG.debug("errors with query:\n" + query); LOG.debug("response:\n" + responseObject.toString()); } return responseObject; }
@Override public void intercept(HttpRequest request) throws IOException { try { token = getToken(); request.getHeaders().setAuthorization("Bearer " + token); } catch (GooglePlayServicesAvailabilityException e) { throw new GooglePlayServicesAvailabilityIOException(e); } catch (UserRecoverableAuthException e) { throw new UserRecoverableAuthIOException(e); } catch (GoogleAuthException e) { throw new GoogleAuthIOException(e); } }
private HttpResponse httpRequest(HttpRequest method, boolean needToken) throws DoubanException, IOException { try { if (needToken) { if (!hasAccessTokenBeenSet()) { throw ErrorHandler.accessTokenNotSet(); } HttpHeaders headers = method.getHeaders(); headers.setAuthorization("Bearer " + this.accessToken); } HttpResponse res = method.execute(); return res; } catch (HttpResponseException ex) { throw ErrorHandler.handleHttpResponseError(ex); } }
public List<User> getUsersSince(int since) throws IOException { HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(API_ENDPOINT + "/users?since=" + since)); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("bearer " + tokenFactory.getToken()); request.setHeaders(headers); HttpResponse response = executeWithRetry(request); // TODO: Handle error status code List<JsonObject> userObjects = Json.createReader(new StringReader(response.parseAsString())).readArray().getValuesAs(JsonObject.class); List<User> users = new ArrayList<>(); for (JsonObject userObject : userObjects) { User user = new User(userObject.getInt("id"), userObject.getString("type")); user.setLogin(userObject.getString("login")); user.setAvatarUrl(userObject.getString("avatar_url")); users.add(user); } return users; }