【问题标题】:Invalid_grant error with google cloud storage service account谷歌云存储服务帐户的 Invalid_grant 错误
【发布时间】:2013-04-18 04:41:41
【问题描述】:

我正在尝试获取 OAuth2 令牌,以便我可以在 Authorization Header 的 GET BUCKET 方法中使用它。

我正在传递下面链接中提到的 grant_type 和断言来获取令牌: https://developers.google.com/accounts/docs/OAuth2ServiceAccount#libraries

是什么导致了 invalid_grant 响应?

谢谢!

【问题讨论】:

    标签: oauth-2.0 google-cloud-storage


    【解决方案1】:

    解决了这个问题。我为内容中需要的“过期”分配了一些不同的值。 现在它可以与

    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 ();
    

    现在只需从连接中获取输入流并解析它以获取 access_token。

    【讨论】:

    • Hiral 这是我能找到的最接近的示例,因为我试图在服务器到服务器应用程序场景中获取有效令牌。您的方法:signData 和 loadKeyFromPkcs12,您是否编写了这些方法,或者它们是否存在于 Cryptography 库中?
    • 我写了这两种方法,它不是密码库的一部分
    【解决方案2】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      相关资源
      最近更新 更多