google-idtoken验证
This commit is contained in:
@@ -1,23 +1,25 @@
|
||||
package com.accompany.payment.google;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.http.HttpRequestInitializer;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.client.util.Preconditions;
|
||||
import com.google.api.client.util.Strings;
|
||||
import com.google.api.services.androidpublisher.AndroidPublisher;
|
||||
import com.google.api.services.androidpublisher.AndroidPublisherScopes;
|
||||
import com.google.auth.http.HttpCredentialsAdapter;
|
||||
import com.google.auth.oauth2.GoogleCredentials;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -33,9 +35,6 @@ public class AndroidPublisherHelper {
|
||||
|
||||
private static final Log log = LogFactory.getLog(AndroidPublisherHelper.class);
|
||||
|
||||
/** Global instance of the JSON factory. */
|
||||
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
|
||||
|
||||
/** Global instance of the HTTP transport. */
|
||||
private static HttpTransport HTTP_TRANSPORT;
|
||||
|
||||
@@ -45,6 +44,7 @@ public class AndroidPublisherHelper {
|
||||
private static volatile String oldPackageName;
|
||||
private static volatile String oldCredentialJson;
|
||||
|
||||
private static final GsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
public static void init(String applicationName, String packageName, String json) throws IOException, GeneralSecurityException {
|
||||
if (needInit(applicationName, packageName, json)) {
|
||||
@@ -56,14 +56,20 @@ public class AndroidPublisherHelper {
|
||||
|
||||
List<String> scopes = new ArrayList<>();
|
||||
scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER);
|
||||
ByteArrayResource resource = new ByteArrayResource(json.getBytes());
|
||||
GoogleCredential credential = GoogleCredential.fromStream(resource.getInputStream())
|
||||
.createScoped(scopes);
|
||||
// 1. 使用HttpCredentialsAdapter包装GoogleCredentials
|
||||
GoogleCredentials credentials = GoogleCredentials.fromStream(
|
||||
new ByteArrayInputStream(json.getBytes()))
|
||||
.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));
|
||||
|
||||
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
|
||||
|
||||
newTrustedTransport();
|
||||
|
||||
//使用谷歌凭据和收据从谷歌获取购买信息
|
||||
androidPublisher = new AndroidPublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
|
||||
// 2. 构建AndroidPublisher实例
|
||||
androidPublisher = new AndroidPublisher.Builder(
|
||||
HTTP_TRANSPORT,
|
||||
JSON_FACTORY,
|
||||
requestInitializer)
|
||||
.setApplicationName(applicationName)
|
||||
.build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user