Skip to content

Commit d289deb

Browse files
committed
Removed pointless try/catch from App Identity sample.
Also, fixes typo (two spaces by Bearer).
1 parent 8d133db commit d289deb

File tree

1 file changed

+36
-35
lines changed
  • appengine/appidentity/src/main/java/com/example/appengine/appidentity

1 file changed

+36
-35
lines changed

appengine/appidentity/src/main/java/com/example/appengine/appidentity/UrlShortener.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.appengine.api.appidentity.AppIdentityService;
1919
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
2020
import com.google.common.io.CharStreams;
21+
2122
import org.json.JSONObject;
2223
import org.json.JSONTokener;
2324

@@ -32,46 +33,46 @@
3233
@SuppressWarnings("serial")
3334
class UrlShortener {
3435
// [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+
*/
3541
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);
4449

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());
5156

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();
5560

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()));
7175
}
72-
} catch (Exception e) {
73-
// Error handling elided.
74-
throw e;
7576
}
7677
}
7778
// [END asserting_identity_to_Google_APIs]

0 commit comments

Comments
 (0)