java - Accessing Google oAuth giving invalid grant_type (Bad request) -


i searched lot in so, skim through many questions regarding same error, applied whatever suggested, nothing turned out. hence writing here.

i learning how make call google api (say google calendar api). going through google developers tutorial https://developers.google.com/identity/protocols/oauth2serviceaccount

so have created service account in google, created credentials (i want invoke google api own application). after create jwt, singed jwt private key shared google, , making rest post call oauth. here code

public class testgoogleapi {  public static void main(string[] args) throws exception{     string header = "{\"alg\":\"rs256\",\"typ\":\"jwt\"}";     long time = calendar.getinstance().gettimeinmillis()/1000;      string claimset = "{\"iss\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com\"," +                 "\"scope\":\"https://www.googleapis.com/auth/calendar\"," +                 "\"aud\":\"https://www.googleapis.com/oauth2/v3/token\"," +                 "\"exp\":"+ (time+3600) +"," +                 "\"iat\":" + time +"}";       string base64header = new string(base64.encodebase64(header.getbytes()),"utf-8");     string base64claimset = new string(base64.encodebase64(claimset.getbytes()),"utf-8");      string input = base64header + "." + base64claimset;      file f = new file("d:/downloads/privatekey.txt");     fileinputstream fis = new fileinputstream(f);     datainputstream dis = new datainputstream(fis);     byte[] keybytes = new byte[(int)f.length()];     dis.readfully(keybytes);     dis.close();     byte[] key = base64.decodebase64(keybytes);      pkcs8encodedkeyspec spec = new pkcs8encodedkeyspec(key);     keyfactory kf = keyfactory.getinstance("rsa");     privatekey privatekey =  kf.generateprivate(spec);      signature signature = signature.getinstance("sha256withrsa");      signature.initsign(privatekey);     signature.update(input.getbytes());     string base64sign = new string(base64.encodebase64(signature.sign()),"utf-8");      dohttppost("https://www.googleapis.com/oauth2/v3/token", input + "." + base64sign); }  public static void dohttppost(string urlstr, string assertion) throws exception{     url url = new url(urlstr);       httpurlconnection conn = (httpurlconnection) url.openconnection();       conn.setrequestmethod("post");       conn.setdooutput(true);       conn.setdoinput(true);       conn.setusecaches(false);       conn.setallowuserinteraction(false);       conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");        outputstream out = conn.getoutputstream();       writer writer = new outputstreamwriter(out, "utf-8");       writer.write("grant_type");       writer.write("=");       writer.write(urlencoder.encode("urn:ietf:params:oauth:grant-type:jwt-bearer", "utf-8"));       writer.write("&");       writer.write("assertion");       writer.write("=");       writer.write(urlencoder.encode(assertion, "utf-8"));       writer.close();       out.close();        if (conn.getresponsecode() != 200) {           system.out.println(conn.getresponsemessage());       }        bufferedreader rd = new bufferedreader(new inputstreamreader(conn.getinputstream()));       stringbuilder sb = new stringbuilder();       string line;       while ((line = rd.readline()) != null) {         sb.append(line);       }       rd.close();        conn.disconnect();       system.out.println(sb.tostring()); }  } 

i getting 400 (bad request). tried same "chrome's advanced rest client", result same, invalid grant_type (bad request).

{    error: "invalid_grant"    error_description: "bad request" } 

am missing thing here. please me

thanks

finally got resolution. when encoding header , claimset, doing base64 encoding, instead of base64safeurl encoidng. after replacing

base64.encodebase64(some_data); 

with

base64.encodebase64urlsafe(some_data); 

at occurrences in above code, working fine. getting access token , able hit google api oauth token


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -