Skip to content

Add sample for asserting identity to third-party services #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix style errors in the App Identity samples.
I checked for style errors using the following command:

    mvn checkstyle:checkstyle \
        -Dcheckstyle.config.location=google_checks.xml \
        -Dcheckstyle.consoleOutput=true
  • Loading branch information
tswast committed Jan 22, 2016
commit a034ddbdff1194a88d74c0439cbac872baaab3a2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.appidentity;

import com.google.apphosting.api.ApiProxy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.appidentity;

import com.google.appengine.api.appidentity.AppIdentityService;
Expand Down Expand Up @@ -41,8 +42,8 @@ class UrlShortener {
public String createShortUrl(String longUrl) throws Exception {
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/urlshortener");
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
final AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
// The token asserts the identity reported by appIdentity.getServiceAccountName()
JSONObject request = new JSONObject();
request.put("longUrl", longUrl);
Expand All @@ -61,7 +62,7 @@ public String createShortUrl(String longUrl) throws Exception {
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// Note: Should check the content-encoding.
// Any JSON parser can be used; this one is used for illustrative purposes.
JSONTokener response_tokens = new JSONTokener(connection.getInputStream());
JSONTokener responseTokens = new JSONTokener(connection.getInputStream());
JSONObject response = new JSONObject(response_tokens);
return (String) response.get("id");
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.appidentity;

import com.google.appengine.api.users.UserService;
Expand All @@ -36,15 +37,16 @@ public UrlShortenerServlet() {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter w = resp.getWriter();
w.println("<!DOCTYPE html>");
w.println("<meta charset=\"utf-8\">");
w.println("<title>Asserting Identity to Google APIs - App Engine App Identity Example</title>");
w.println("<form method=\"post\">");
w.println("<label for=\"longUrl\">URL:</label>");
w.println("<input id=\"longUrl\" name=\"longUrl\" type=\"text\">");
w.println("<input type=\"submit\" value=\"Shorten\">");
w.println("</form>");
PrintWriter writer = resp.getWriter();
writer.println("<!DOCTYPE html>");
writer.println("<meta charset=\"utf-8\">");
writer.println(
"<title>Asserting Identity to Google APIs - App Engine App Identity Example</title>");
writer.println("<form method=\"post\">");
writer.println("<label for=\"longUrl\">URL:</label>");
writer.println("<input id=\"longUrl\" name=\"longUrl\" type=\"text\">");
writer.println("<input type=\"submit\" value=\"Shorten\">");
writer.println("</form>");
}

@Override
Expand All @@ -57,19 +59,19 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
}

String shortUrl;
PrintWriter w = resp.getWriter();
PrintWriter writer = resp.getWriter();
try {
shortUrl = shortener.createShortUrl(longUrl);
} catch (Exception e) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
w.println("error shortening URL: " + longUrl);
writer.println("error shortening URL: " + longUrl);
e.printStackTrace(w);
return;
}

w.print("long URL: ");
w.println(longUrl);
w.print("short URL: ");
w.println(shortUrl);
writer.print("long URL: ");
writer.println(longUrl);
writer.print("short URL: ");
writer.println(shortUrl);
}
}