use of com.google.api.client.http.UrlEncodedContent in project data-transfer-project by google.
the class OAuth2DataGenerator method generateAuthData.
@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected for OAuth flow");
Preconditions.checkArgument(initialAuthData == null, "Initial auth data not expected for " + config.getServiceName());
Map<String, String> params = new LinkedHashMap<>();
params.put("client_id", clientId);
params.put("client_secret", clientSecret);
params.put("grant_type", "authorization_code");
params.put("redirect_uri", callbackBaseUrl);
params.put("code", authCode);
HttpContent content = new UrlEncodedContent(params);
try {
String tokenResponse = OAuthUtils.makeRawPostRequest(httpTransport, config.getTokenUrl(), content);
return config.getResponseClass(tokenResponse);
} catch (IOException e) {
// TODO
throw new RuntimeException("Error getting token", e);
}
}
use of com.google.api.client.http.UrlEncodedContent in project data-transfer-project by google.
the class MastodonHttpUtilities method postStatus.
/**
* 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);
}
Aggregations