Search in sources :

Example 11 with UrlEncodedContent

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);
    }
}
Also used : UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) IOException(java.io.IOException) HttpContent(com.google.api.client.http.HttpContent) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with UrlEncodedContent

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);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl)

Aggregations

UrlEncodedContent (com.google.api.client.http.UrlEncodedContent)12 GenericUrl (com.google.api.client.http.GenericUrl)9 HttpRequest (com.google.api.client.http.HttpRequest)8 HttpResponse (com.google.api.client.http.HttpResponse)6 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)5 JsonObjectParser (com.google.api.client.json.JsonObjectParser)5 GenericData (com.google.api.client.util.GenericData)5 IOException (java.io.IOException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Date (java.util.Date)2 Credential (com.google.api.client.auth.oauth2.Credential)1 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)1 EmptyContent (com.google.api.client.http.EmptyContent)1 HttpBackOffIOExceptionHandler (com.google.api.client.http.HttpBackOffIOExceptionHandler)1 HttpBackOffUnsuccessfulResponseHandler (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler)1 BackOffRequired (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired)1 HttpContent (com.google.api.client.http.HttpContent)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1