Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle OAuth2 Service API: Issue fetching access token using service email and private key
    primarykey
    data
    text
    <p>Tried fetching the access token using JWT as well as Java Auth client API call but did not succeed.</p> <p>Using Java client API code below , I get Exception "java.net.SocketTimeoutException: connect timed out".</p> <pre><code> GoogleCredential credential = new GoogleCredential.Builder() .setTransport(HTTP_TRANSPORT) .setJsonFactory(JSON_FACTORY) .setServiceAccountId("@developer.gserviceaccount.com") .setServiceAccountPrivateKeyFromP12File(new File("&lt;publickey&gt;-privatekey.p12")) .setServiceAccountScopes("https://www.googleapis.com/auth/devstorage.full_control") .build(); credential.refreshToken(); System.out.println("access token : " + credential.getAccessToken()); System.out.println("refresh token : " + credential.getRefreshToken()); </code></pre> <p>If we remove call to "credential.refreshtoken()", it returns null access key and refresh key. Could someone please guide me and tell where I am going wrong?</p> <p>Also using direct JWT token for authentication as listed below, I get response "{"error" : "invalid_grant"}" with Error code 400 :</p> <pre><code> private static PrivateKey getPrivateKey(String keyFile, String alias, String password) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException { KeyStore keystore = KeyStore.getInstance("PKCS12"); keystore.load(new FileInputStream(keyFile), password.toCharArray()); PrivateKey privateKey = (PrivateKey) keystore.getKey(alias, password.toCharArray()); return privateKey; } public static String sign(PrivateKey privateKey, JsonFactory jsonFactory, String header, String payload) throws Exception { System.out.println("header : " + header + " : " + Base64.encodeBase64URLSafeString(header.getBytes())); System.out.println("payload : " + payload + " : " + Base64.encodeBase64URLSafeString(payload.getBytes())); String content = Base64.encodeBase64URLSafeString(header.getBytes()) + "." + Base64.encodeBase64URLSafeString(payload.getBytes()); byte[] contentBytes = StringUtils.getBytesUtf8(content); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initSign(privateKey); signer.update(contentBytes); byte[] signature = signer.sign(); String signedContent = content + "." + Base64.encodeBase64URLSafeString(signature); System.out.println("signedContent:" + signedContent); return signedContent; } public static void main(String args[]) { HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); JsonFactory JSON_FACTORY = new JacksonFactory(); String header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"} "; String payload = "{\"iss\":\"*******@developer.gserviceaccount.com\",\"scope\":\"https://www.googleapis.com/auth/devstorage.full_control\",\"aud\":\"https://accounts.google.com/o/oauth2/token\",\"exp\": " + new Date().getTime() + ",\"iat\":" + (new Date().getTime() + 3600000) + "}"; PrivateKey serviceAccountPrivateKey = getPrivateKey("&lt;public_key&gt;-privatekey.p12", "privatekey", "notasecret"); String assertion = sign(serviceAccountPrivateKey, JSON_FACTORY, header, payload); } </code></pre> <p>Later passing this assertion to API "<a href="https://accounts.google.com/o/oauth2/token" rel="nofollow">https://accounts.google.com/o/oauth2/token</a>" with this generated assertion and grant type "urn:ietf:params:oauth:grant-type:jwt-bearer" , gives response "{"error" : "invalid_grant"}" with Error code 400.</p> <p>Could someone please help me and suggest where I am going wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload