Код: Выделить всё
Status Code: HTTP/1.1 400 Bad Request
Response: {"error":"invalid_grant","error_description":"Invalid grant: account not found"}
У меня есть используя переработанный код других людей, сталкивавшихся с подобными проблемами. Я использую компоненты apachehttp для веб-сервисов и io.jsonwebtoken для создания JWT. Я новичок в работе с этими библиотеками и Java в целом, поэтому мне было интересно, есть ли какие-либо явные проблемы с функциональной точки зрения, которые я пропустил.
Код: Выделить всё
import io.jsonwebtoken.*
import io.jsonwebtoken.security.Keys;
import org.apache.commons.codec.binary.Base64
import org.apache.http.client.methods.*
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import java.security.Key;
import java.security.PrivateKey
import java.util.Base64
import java.util.Base64.Decoder
import java.nio.charset.StandardCharsets
import java.security.interfaces.*
import java.security.KeyFactory
import java.security.NoSuchAlgorithmException
import java.security.spec.PKCS8EncodedKeySpec
import java.security.Key;
iss = "serviceacct@project.iam.gserviceaccount.com" // svc account email
scope = "https://www.googleapis.com/auth/compute.readonly" // A space-delimited list of the permissions that the application requests.
aud = "https://oauth2.googleapis.com/token" // ALWAYS
def iat = (System.currentTimeMillis() / 1000).trunc()
// // The time the assertion was issued, specified as seconds since 00:00:00 UTC, January 1, 1970.
def exp = (System.currentTimeMillis() / 1000 + 1200).trunc()
//20 minutes from now // The expiration time of the assertion, specified as seconds since 00:00:00 UTC, January 1, 1970. This value has a maximum of 1 hour after the issued time.
//JWT Payload
String jsonString = """{"typ":"${iss}",
"scope":"${scope}",
"aud":"${aud}",
"exp":"${exp}",
"iat":"${iat}"}""";
// Formatting key
StringBuilder pkcs8Lines = new StringBuilder();
BufferedReader rdr = new BufferedReader(new StringReader(BASEKEY));
String line;
while ((line = rdr.readLine()) != null) {
pkcs8Lines.append(line);
}
// Cleaning key
String pkcs8Pem = pkcs8Lines.toString();
pkcs8Pem = pkcs8Pem.replace("-----BEGIN PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replace("-----END PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replaceAll("\\s+","");
// Decoding key
byte[] valueDecoded = Base64.decodeBase64(pkcs8Pem);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(valueDecoded);
KeyFactory rsaFact = KeyFactory.getInstance("RSA");
RSAPrivateKey key = (RSAPrivateKey) rsaFact.generatePrivate(keySpec);
// Generate Token
String jwt = Jwts.builder()
//JWT Header
.setHeaderParam("alg","RS256")
.setHeaderParam("typ","JWT")
.setPayload(jsonString)
.signWith(SignatureAlgorithm.RS256, key)
.compact();
def httpClient = HttpClientBuilder.create().build()
def httpPost = new HttpPost(aud)
String bodystring = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion="+"${jwt}"
String encodedParams = URLEncoder.encode(bodystring, StandardCharsets.UTF_8); // Encode
StringEntity entity = new StringEntity(bodystring)
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded")
def response = httpClient.execute(httpPost)
//debug
String statusCode = response.getStatusLine() //.getStatusCode();
String content = response.getEntity().getContent().getText() //.getStatusCode();
System.out.println("Status Code: " + statusCode);
System.out.println("Response: " + content);
Что-то я Проблема, скорее всего, заключается в том, что я не могу установить кодировку в формате url-form-encoded. когда я отлаживаю с помощью
Код: Выделить всё
System.out.println(entity.getContentType());Подробнее здесь: https://stackoverflow.com/questions/787 ... ogle-cloud