|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.example.appengine.appidentity; |
| 17 | + |
| 18 | +import com.google.appengine.api.appidentity.AppIdentityService; |
| 19 | +import com.google.appengine.api.appidentity.AppIdentityServiceFactory; |
| 20 | +import com.google.common.io.CharStreams; |
| 21 | + |
| 22 | +import org.json.JSONObject; |
| 23 | +import org.json.JSONTokener; |
| 24 | + |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.io.OutputStreamWriter; |
| 28 | +import java.net.HttpURLConnection; |
| 29 | +import java.net.URL; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.ArrayList; |
| 32 | + |
| 33 | +@SuppressWarnings("serial") |
| 34 | +class UrlShortener { |
| 35 | + // [START asserting_identity_to_Google_APIs] |
| 36 | + /** |
| 37 | + * Returns a shortened URL by calling the Google URL Shortener API. |
| 38 | + * |
| 39 | + * <p>Note: Error handling elided for simplicity. |
| 40 | + */ |
| 41 | + public String createShortUrl(String longUrl) throws Exception { |
| 42 | + ArrayList<String> scopes = new ArrayList<String>(); |
| 43 | + scopes.add("https://www.googleapis.com/auth/urlshortener"); |
| 44 | + AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); |
| 45 | + AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes); |
| 46 | + // The token asserts the identity reported by appIdentity.getServiceAccountName() |
| 47 | + JSONObject request = new JSONObject(); |
| 48 | + request.put("longUrl", longUrl); |
| 49 | + |
| 50 | + URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1"); |
| 51 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 52 | + connection.setDoOutput(true); |
| 53 | + connection.setRequestMethod("POST"); |
| 54 | + connection.addRequestProperty("Content-Type", "application/json"); |
| 55 | + connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken()); |
| 56 | + |
| 57 | + OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); |
| 58 | + request.write(writer); |
| 59 | + writer.close(); |
| 60 | + |
| 61 | + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { |
| 62 | + // Note: Should check the content-encoding. |
| 63 | + // Any JSON parser can be used; this one is used for illustrative purposes. |
| 64 | + JSONTokener response_tokens = new JSONTokener(connection.getInputStream()); |
| 65 | + JSONObject response = new JSONObject(response_tokens); |
| 66 | + return (String) response.get("id"); |
| 67 | + } else { |
| 68 | + try (InputStream s = connection.getErrorStream(); |
| 69 | + InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8)) { |
| 70 | + throw new RuntimeException(String.format( |
| 71 | + "got error (%d) response %s from %s", |
| 72 | + connection.getResponseCode(), |
| 73 | + CharStreams.toString(r), |
| 74 | + connection.toString())); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + // [END asserting_identity_to_Google_APIs] |
| 79 | +} |
0 commit comments