gpt4 book ai didi

download - 使用 Google Sign-in 登录网络服务器

转载 作者:行者123 更新时间:2023-12-04 20:29:03 26 4
gpt4 key购买 nike

我正在从 HTTP 客户端(Java 或 Dart - Android 应用程序)连接到 3rd 方 Web 服务器,以下载属于该服务器上当前用户的一些资源(XML 或 IMG 文件)。此站点需要使用 Google Sing-In 登录。我在我的 Android 应用程序中设置了所有内容以使用 Google 登录用户,我获得了他们的授权 idToken。但是如何在 HTTP GET 或 POST 方法中实际使用它来下载 protected 资源?

使用 BASIC 身份验证很容易 - 只需正确设置 HTTP 'Authorization' header (“Basic”+ 用户:密码编码为 base64),调用 GET,然后我下载所需的资源。但是我找不到有关如何使用 Google Sing-In 执行此操作的任何信息。我是否会在某些 header 中发送从 Google 收到的 idToken?还需要什么魔法?

最佳答案

添加一个Java代码片段,希望它有帮助:

// (Receive authCode via HTTPS POST)


if (request.getHeader('X-Requested-With') == null) {
// Without the `X-Requested-With` header, this request could be forged. Aborts.
}

// Set path to the Web application client_secret_*.json file you downloaded from the
// Google API Console: https://console.developers.google.com/apis/credentials
// You can also find your Web application client ID and client secret from the
// console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
// object.
String CLIENT_SECRET_FILE = "/path/to/client_secret.json";

// Exchange auth code for access token
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode,
REDIRECT_URI) // Specify the same redirect URI that you use with your web
// app. If you don't have a web version of your app, you can
// specify an empty string.
.execute();

String accessToken = tokenResponse.getAccessToken();

// Use access token to call API
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Drive drive =
new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
.setApplicationName("Auth Code Exchange Demo")
.build();
File file = drive.files().get("appfolder").execute();

// Get profile info from ID token
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
String userId = payload.getSubject(); // Use this value as a key to identify a user.
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");

有关详细信息,请在以下位置找到所有必需的步骤和引用: https://developers.google.com/identity/sign-in/web/server-side-flow#step_1_create_a_client_id_and_client_secret

关于download - 使用 Google Sign-in 登录网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com