|
18 | 18 | import com.google.appengine.api.appidentity.AppIdentityService;
|
19 | 19 | import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
|
20 | 20 | import com.google.common.io.CharStreams;
|
| 21 | + |
21 | 22 | import org.json.JSONObject;
|
22 | 23 | import org.json.JSONTokener;
|
23 | 24 |
|
|
32 | 33 | @SuppressWarnings("serial")
|
33 | 34 | class UrlShortener {
|
34 | 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 | + */ |
35 | 41 | public String createShortUrl(String longUrl) throws Exception {
|
36 |
| - try { |
37 |
| - ArrayList<String> scopes = new ArrayList<String>(); |
38 |
| - scopes.add("https://www.googleapis.com/auth/urlshortener"); |
39 |
| - AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); |
40 |
| - AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes); |
41 |
| - // The token asserts the identity reported by appIdentity.getServiceAccountName() |
42 |
| - JSONObject request = new JSONObject(); |
43 |
| - request.put("longUrl", longUrl); |
| 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); |
44 | 49 |
|
45 |
| - URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1"); |
46 |
| - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
47 |
| - connection.setDoOutput(true); |
48 |
| - connection.setRequestMethod("POST"); |
49 |
| - connection.addRequestProperty("Content-Type", "application/json"); |
50 |
| - connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken()); |
| 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()); |
51 | 56 |
|
52 |
| - OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); |
53 |
| - request.write(writer); |
54 |
| - writer.close(); |
| 57 | + OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); |
| 58 | + request.write(writer); |
| 59 | + writer.close(); |
55 | 60 |
|
56 |
| - if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { |
57 |
| - // Note: Should check the content-encoding. |
58 |
| - // Any JSON parser can be used; this one is used for illustrative purposes. |
59 |
| - JSONTokener response_tokens = new JSONTokener(connection.getInputStream()); |
60 |
| - JSONObject response = new JSONObject(response_tokens); |
61 |
| - return (String) response.get("id"); |
62 |
| - } else { |
63 |
| - try (InputStream s = connection.getErrorStream(); |
64 |
| - InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8)) { |
65 |
| - throw new RuntimeException(String.format( |
66 |
| - "got error (%d) response %s from %s", |
67 |
| - connection.getResponseCode(), |
68 |
| - CharStreams.toString(r), |
69 |
| - connection.toString())); |
70 |
| - } |
| 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())); |
71 | 75 | }
|
72 |
| - } catch (Exception e) { |
73 |
| - // Error handling elided. |
74 |
| - throw e; |
75 | 76 | }
|
76 | 77 | }
|
77 | 78 | // [END asserting_identity_to_Google_APIs]
|
|
0 commit comments