Oauth 2.0 google云存储服务帐户出现无效的\u授权错误

Oauth 2.0 google云存储服务帐户出现无效的\u授权错误,oauth-2.0,google-cloud-storage,Oauth 2.0,Google Cloud Storage,我正在尝试获取OAuth2令牌,以便可以在授权头中的get BUCKET方法中使用它 我正在传递下面链接中提到的grant_类型和断言以获取令牌: 是什么导致响应中的无效_授权 谢谢 解决了这个问题。我给内容中需要的“过期”指定了一些不同的值。 现在它与 long currenttime = System.currentTimeMillis(); long now = currenttime / 1000; long expiration = currenttime / 1000 + 3600

我正在尝试获取OAuth2令牌,以便可以在授权头中的get BUCKET方法中使用它

我正在传递下面链接中提到的grant_类型和断言以获取令牌:

是什么导致响应中的无效_授权


谢谢

解决了这个问题。我给内容中需要的“过期”指定了一些不同的值。 现在它与

long currenttime = System.currentTimeMillis();
long now = currenttime / 1000;
long expiration = currenttime / 1000 + 3600;
并有以下几点

String temp = JWTBase64Header + "." + JWTBase64Content;
byte[] JWTSignatureInput = temp.getBytes("UTF8");

final String keyFile = "xx9d9xxxxxx12cd99fxxxx60bxxxxx1-privatekey.p12";
final String keyPassword = "notasecret";
PrivateKey pkcsKey = loadKeyFromPkcs12(keyFile, keyPassword.toCharArray());
String JWTBase64Signature = signData(pkcsKey, new String(JWTSignatureInput, "UTF8"));

String params = "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion="+JWTBase64Header + "." + JWTBase64Content + "."
        + JWTBase64Signature;

URL url = new URL("https://accounts.google.com/o/oauth2/token");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));

//Send request

  DataOutputStream wr = new DataOutputStream (
              conn.getOutputStream ());
  wr.writeBytes (params.toString());
  wr.flush ();
  wr.close ();

现在只需从连接中获取inputstream并对其进行解析以获取访问令牌。

解决了这个问题。我给内容中需要的“过期”指定了一些不同的值。
private static PrivateKey loadKeyFromPkcs12(String filename, char[] password)
        throws Exception {
    FileInputStream fis = new FileInputStream(
            "NewFolder/" + filename);

    KeyStore ks = KeyStore.getInstance("PKCS12");
    try {
        ks.load(fis, password);
    } catch (IOException e) {
        if (e.getCause() != null
                && e.getCause() instanceof UnrecoverableKeyException) {
            System.err.println("Incorrect password");
        }
        throw e;
    }
    return (PrivateKey) ks.getKey("privatekey", password);
}

private static String signData(PrivateKey key, String data)
        throws Exception {
    Signature signer = Signature.getInstance("SHA256withRSA");
    signer.initSign(key);
    signer.update(data.getBytes("UTF8"));
    byte[] rawSignature = signer.sign();
    String encodedSignature = new String(Base64.encodeBase64URLSafe(rawSignature));
    return encodedSignature;
}
现在它与

long currenttime = System.currentTimeMillis();
long now = currenttime / 1000;
long expiration = currenttime / 1000 + 3600;
并有以下几点

String temp = JWTBase64Header + "." + JWTBase64Content;
byte[] JWTSignatureInput = temp.getBytes("UTF8");

final String keyFile = "xx9d9xxxxxx12cd99fxxxx60bxxxxx1-privatekey.p12";
final String keyPassword = "notasecret";
PrivateKey pkcsKey = loadKeyFromPkcs12(keyFile, keyPassword.toCharArray());
String JWTBase64Signature = signData(pkcsKey, new String(JWTSignatureInput, "UTF8"));

String params = "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion="+JWTBase64Header + "." + JWTBase64Content + "."
        + JWTBase64Signature;

URL url = new URL("https://accounts.google.com/o/oauth2/token");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));

//Send request

  DataOutputStream wr = new DataOutputStream (
              conn.getOutputStream ());
  wr.writeBytes (params.toString());
  wr.flush ();
  wr.close ();

现在,只需从连接获取inputstream并对其进行解析以获取访问令牌。

Hiral这是我能找到的最接近的示例,因为我正在尝试在服务器到服务器应用程序场景中获取有效令牌。您的方法:signData和loadKeyFromPkcs12,您编写了这些方法吗,或者它们是否存在于加密库中?我编写了这两个方法,它不是加密库的一部分Hiral这是我能找到的最接近的示例,因为我正试图在服务器到服务器应用程序场景中获取有效令牌。您的方法:signData和loadKeyFromPkcs12,您编写了这些方法吗,或者它们是否存在于加密库中?我编写了这两个方法,它不是加密库的一部分
private static PrivateKey loadKeyFromPkcs12(String filename, char[] password)
        throws Exception {
    FileInputStream fis = new FileInputStream(
            "NewFolder/" + filename);

    KeyStore ks = KeyStore.getInstance("PKCS12");
    try {
        ks.load(fis, password);
    } catch (IOException e) {
        if (e.getCause() != null
                && e.getCause() instanceof UnrecoverableKeyException) {
            System.err.println("Incorrect password");
        }
        throw e;
    }
    return (PrivateKey) ks.getKey("privatekey", password);
}

private static String signData(PrivateKey key, String data)
        throws Exception {
    Signature signer = Signature.getInstance("SHA256withRSA");
    signer.initSign(key);
    signer.update(data.getBytes("UTF8"));
    byte[] rawSignature = signer.sign();
    String encodedSignature = new String(Base64.encodeBase64URLSafe(rawSignature));
    return encodedSignature;
}