400 Fehler bei der Überprüfung der Herausforderungen bei der verifizierten Access API von GoogleJava

Java-Forum
Anonymous
 400 Fehler bei der Überprüfung der Herausforderungen bei der verifizierten Access API von Google

Post by Anonymous »

Ich verwende den folgenden Code, um eine Herausforderung Antwort an die API von Google zur Überprüfung zu senden. Ich stoße jedoch auf einen Fehler: < /p>

Code: Select all

Verification Failed: 400 Bad Request
Response: {
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT"
}
}
< /code>
Code: < /p>
package org.example;

import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import okhttp3.*;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Collections;

public class Main {

private static final String VERIFIED_ACCESS_URL = "https://verifiedaccess.googleapis.com/v2/challenge:verify";
// Fetch sensitive data from environment variables
private static final String SERVICE_ACCOUNT_KEY = System.getenv("SERVICE_ACCOUNT_KEY");
private static final String CHALLENGE_RESPONSE = System.getenv("CHALLENGE_RESPONSE");
private static final String EXPECTED_IDENTITY = "KIOSK_MODE"; // Optional, set if needed

public static void main(String[] args) {
try {
// Validate the challenge response
String sanitizedResponse = CHALLENGE_RESPONSE.trim();
if (!isValidBase64(sanitizedResponse)) {
System.err.println("Invalid Base64 encoding in challenge response.");
return;
}

String accessToken = getAccessToken();

// Prepare the payload
JSONObject payloadJson = new JSONObject();
payloadJson.put("challengeResponse", sanitizedResponse);
if (!EXPECTED_IDENTITY.isEmpty()) {
payloadJson.put("expectedIdentity", EXPECTED_IDENTITY);
}

String payload = payloadJson.toString();
System.out.println("Payload: " + payload);

OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(payload, MediaType.parse("application/json"));

Request request = new Request.Builder()
.url(VERIFIED_ACCESS_URL)
.addHeader("Authorization", "Bearer " + accessToken)
.addHeader("Content-Type", "application/json")
.post(body)
.build();

// Execute the request
try (Response response = client.newCall(request).execute()) {
String responseBody = response.body() != null ? response.body().string() : "No response body";
System.out.println("HTTP Response Code: " + response.code());
System.out.println("Response Body: " + responseBody);

if (response.isSuccessful()) {
System.out.println("Verification Successful!");
} else {
System.err.println("Verification Failed. Reason: " + responseBody);
}
}
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
e.printStackTrace();
}
}

private static String getAccessToken() throws IOException {
GoogleCredentials credentials = GoogleCredentials.fromStream(
new ByteArrayInputStream(SERVICE_ACCOUNT_KEY.getBytes()))
.createScoped(Collections.singletonList("https://www.googleapis.com/auth/verifiedaccess"));
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
if (token == null) {
throw new RuntimeException("Failed to fetch the access token.");
}
return token.getTokenValue();
}

private static boolean isValidBase64(String input) {
try {
Base64.getDecoder().decode(input);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}
Ich folgte der Dokumentation des Google Verified Access: https://developers JSON -Format. Alle Vorschläge zur Lösung dieses Problems wären sehr geschätzt! Danke.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post