在 Scala 中使用 Google 的 Java 身份验证?

Using Google's Java authentication within Scala?

我目前正在尝试在 Scala 后端构建 GPlay 身份验证服务。客户端通过他们的 ID Token 和 salt 发送;从这里开始,我尝试使用 Google's Backend Auth documentation 来验证用户的 GPlay 帐户。

正如预期的那样,Google 的 Java 库都可以毫无问题地用于 Scala。

Java/Scala 进口:

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;

Java:

GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
    .setAudience(Collections.singletonList(CLIENT_ID))
    .build();

斯卡拉:

val verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
  .setAudience(Collections.singletonList(CLIENT_ID))
  .build();

他们的指南似乎遗漏了很多东西——即,应该为传输、jsonFactory 和 CLIENT_ID 变量实例化什么。它也没有提及为什么 CLIENT_ID 应该存储在 singletonList 中,而不是像我们通常在客户端上实现的那样只是一个字符串。

目前,我收到(如预期)"cannot resolve symbol" 传输、jsonFactory 和 CLIENT_ID 错误。我对 Collections 出现了同样的错误,但我认为那是因为我没有尝试创建任何集合——因为文档中没有给出关于我们应该寻找什么来实例化一个集合的指示。

如能提供任何帮助,我们将不胜感激。

我从这里获取了以下代码:https://developers.google.com/api-client-library/java/google-api-java-client/oauth2

GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Plus plus = new Plus.builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
    .setApplicationName("Google-PlusSample/1.0")
    .build();

您可以在此处查看有关如何创建所需的传输变量和 jsonFactory 变量的示例。

我已经在 Java 上创建了令牌验证功能,以备不时之需,下面是一个示例:

public static Optional<GoogleIdToken> validateGoogleToken(final String idTokenString) {
        try {
            Optional<String> clientIDConfValueOptional = PlayUtil.getConfValue("GOOGLE_CLIENT_ID");
            if (clientIDConfValueOptional.isPresent()) {

                //PLEASE NOTE!!!!
                //for ANDROID with PlayServices 8.3 + issuer=https://accounts.google.com
                //OTHERWISE issuer=accounts.google.com
                //the otherwise is without "https://"
                GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(
                       MiscUtil.NET_HTTP_TRANSPORT, JacksonFactory.getDefaultInstance())
                        .setAudience(Arrays.asList(clientIDConfValueOptional.get()))
                        // If you retrieved the token on Android using the Play Services 8.3 API 
                        //                        or newer, set
                        // the issuer to "https://accounts.google.com". Otherwise, set the issuer to
                        // "accounts.google.com". If you need to verify tokens from multiple sources, build
                        // a GoogleIdTokenVerifier for each issuer and try them both.
                        .setIssuer("accounts.google.com")
                        .build();

                GoogleIdToken googleIdToken = verifier.verify(idTokenString);
                if (googleIdToken != null) {
                    Payload payload = googleIdToken.getPayload();

                    // Print user identifier
                    String userId = payload.getSubject();
//                    System.out.println("User ID: " + userId);

                    // Get profile information from payload
//                    String email = payload.getEmail();
//                    System.out.println("email = " + email);
//                    boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
//                    System.out.println("emailVerified = " + emailVerified);
//                    String name = (String) payload.get("name");
//                    System.out.println("name = " + name);
//                    String pictureUrl = (String) payload.get("picture");
//                    System.out.println("pictureUrl = " + pictureUrl);
//                    String locale = (String) payload.get("locale");
//                    System.out.println("locale = " + locale);
//                    String familyName = (String) payload.get("family_name");
//                    System.out.println("familyName = " + familyName);
//                    String givenName = (String) payload.get("given_name");
//                    System.out.println("givenName = " + givenName);

                    // Use or store profile information
                    // ...
                    return Optional.ofNullable(googleIdToken);
                } else {
                    System.out.println("Invalid ID token.");
                }
            } else {
                throw new IllegalArgumentException("CLIENT_ID key can't be found on application.conf file");
            }
        } catch (GeneralSecurityException | IOException ex) {
            Logger.error("Error: ", ex);
        }
        return Optional.empty();
    }

你可能知道如何将这段代码从 Java 翻译成 Scala 无论如何如果你需要一些帮助你可以使用这样的服务:java to scala converter

希望这对您有所帮助。