本文整理汇总了Java中com.google.appengine.api.appidentity.AppIdentityServiceFactory.getAppIdentityService方法的典型用法代码示例。如果您正苦于以下问题:Java AppIdentityServiceFactory.getAppIdentityService方法的具体用法?Java AppIdentityServiceFactory.getAppIdentityService怎么用?Java AppIdentityServiceFactory.getAppIdentityService使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.appengine.api.appidentity.AppIdentityServiceFactory
的用法示例。
在下文中一共展示了AppIdentityServiceFactory.getAppIdentityService方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createFirebaseToken
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
/**
* Create a secure JWT token for the given userId.
*/
public String createFirebaseToken(Game game, String userId) {
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
final BaseEncoding base64 = BaseEncoding.base64();
String header = base64.encode("{\"typ\":\"JWT\",\"alg\":\"RS256\"}".getBytes());
// Construct the claim
String channelKey = game.getChannelKey(userId);
String clientEmail = appIdentity.getServiceAccountName();
long epochTime = System.currentTimeMillis() / 1000;
long expire = epochTime + 60 * 60; // an hour from now
Map<String, Object> claims = new HashMap<String, Object>();
claims.put("iss", clientEmail);
claims.put("sub", clientEmail);
claims.put("aud", IDENTITY_ENDPOINT);
claims.put("uid", channelKey);
claims.put("iat", epochTime);
claims.put("exp", expire);
String payload = base64.encode(new Gson().toJson(claims).getBytes());
String toSign = String.format("%s.%s", header, payload);
AppIdentityService.SigningResult result = appIdentity.signForApp(toSign.getBytes());
return String.format("%s.%s", toSign, base64.encode(result.getSignature()));
}
示例2: createShortUrl
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
/**
* Returns a shortened URL by calling the Google URL Shortener API.
*
* <p>Note: Error handling elided for simplicity.
*/
public String createShortUrl(String longUrl) throws Exception {
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/urlshortener");
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);
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
request.write(writer);
writer.close();
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 responseTokens = new JSONTokener(connection.getInputStream());
JSONObject response = new JSONObject(responseTokens);
return (String) response.get("id");
} else {
try (InputStream s = connection.getErrorStream();
InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8)) {
throw new RuntimeException(String.format(
"got error (%d) response %s from %s",
connection.getResponseCode(),
CharStreams.toString(r),
connection.toString()));
}
}
}
示例3: createShortUrl
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
/**
* Returns a shortened URL by calling the Google URL Shortener API.
*
* <p>Note: Error handling elided for simplicity.
*/
public String createShortUrl(String longUrl) throws Exception {
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/urlshortener");
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);
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
request.write(writer);
writer.close();
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 responseTokens = new JSONTokener(connection.getInputStream());
JSONObject response = new JSONObject(responseTokens);
return (String) response.get("id");
} else {
try (InputStream s = connection.getErrorStream();
InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8)) {
throw new RuntimeException(
String.format(
"got error (%d) response %s from %s",
connection.getResponseCode(), CharStreams.toString(r), connection.toString()));
}
}
}
示例4: getProjectId
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
/**
* Returns the project ID.
*
* @return the project ID.
*/
public static String getProjectId() {
AppIdentityService identityService =
AppIdentityServiceFactory.getAppIdentityService();
// The project ID associated to an app engine application is the same
// as the app ID.
return identityService.parseFullAppId(ApiProxy.getCurrentEnvironment()
.getAppId()).getId();
}
示例5: CloudFileManager
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
public CloudFileManager() {
AppIdentityService appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
defaultBucket = appIdentityService.getDefaultGcsBucketName();
productionManifestFile = new GcsFilename(defaultBucket, Config.MANIFEST_NAME);
stagingManifestFile = new GcsFilename(defaultBucket, Config.MANIFEST_NAME_STAGING);
}
示例6: SignForAppServlet
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
public SignForAppServlet() {
appIdentity = AppIdentityServiceFactory.getAppIdentityService();
}
示例7: AppIdentityAccessTokenProvider
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
public AppIdentityAccessTokenProvider() {
this.appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
}
示例8: setUp
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
@Before
public void setUp() {
appIdentity = AppIdentityServiceFactory.getAppIdentityService();
}
示例9: getAccessToken
import com.google.appengine.api.appidentity.AppIdentityServiceFactory; //导入方法依赖的package包/类
/**
* Get the access token for a list of scopes.
*
* @param scopes the scopes to get the access tokens for.
* @return the access token.
*/
public static String getAccessToken(List<String> scopes) {
final AppIdentityService appIdService = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult result = appIdService.getAccessToken(scopes);
return result.getAccessToken();
}
开发者ID:GoogleCloudPlatform,项目名称:solutions-google-compute-engine-orchestrator,代码行数:12,代码来源:GceApiUtils.java