java - Google Plus SignIn / oAuth2 - server side throwing TokenResponseException: 401 Unauthorized -
so i'm building simple system composed of android application , java ee restful service, , i'm having terrible problems authorization google. i'm implementing google+ sign-in , works on client side, is, able obtain user email, jwt idtoken, , server authentication code, i'd exchange access , refresh tokens , store them in database. done follow:
mgoogleapiclient = new googleapiclient.builder(this) .addapi(plus.api) .addscope(plus.scope_plus_login) .addscope(plus.scope_plus_profile) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .requestserverauthcode(service_client_id, this) .build(); // ... mgoogleapiclient.connect();
after successful connection onuploadserverauthcode called.
@override public boolean onuploadserverauthcode(string idtoken, string serverauthcode) { //... //when it's called, send serverauthcode server. }
then on server side use code here: https://developers.google.com/drive/web/credentials
string clientsecret_location = "/web-inf/classes/client_secret.json"; string redirect_uri = "urn:ietf:wg:oauth:2.0:oob"; list<string> scopes = arrays.aslist("https://www.googleapis.com/auth/plus.login"); googleauthorizationcodeflow getflow() throws ioexception { if (flow == null) { inputstream in = context.getresourceasstream(clientsecret_location); googleclientsecrets clientsecret = googleclientsecrets.load( json_factory, new inputstreamreader(in)); flow = new googleauthorizationcodeflow.builder(http_transport, json_factory, clientsecret, scopes) .setaccesstype("offline").setapprovalprompt("force") .build(); } return flow; } credential exchangecode(string authorizationcode) throws codeexchangeexception { try { googleauthorizationcodeflow flow = getflow(); googletokenresponse response = flow .newtokenrequest(authorizationcode) .setredirecturi(redirect_uri).execute(); return flow.createandstorecredential(response, null); } catch (ioexception e) { system.err.println("an error occurred: " + e); throw new codeexchangeexception(null); } }
and i'm stuck there.
if use: client_secret.json android app client id:
{ "installed":{ "auth_uri":"https://accounts.google.com/o/oauth2/auth", "token_uri":"https://accounts.google.com/o/oauth2/token", "client_email":"", "redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"], "client_x509_cert_url":"", "client_id":"243714256753-lqcm63mxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs" } }
in exchangecode(string)
googleauthorizationcodetokenrequest.execute()
throws tokenresponseexception: 401 unauthorized
and if use: client_secret.json service account client id:
{ "private_key_id": "77bee9dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "private_key": "-----begin private key-----\nmiicdgibadanbgk ... \u003d\u003d\n-----end private key-----\n", "client_email": "243714256753-g21p1xxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com", "client_id": "243714256753-g21p1xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", "type": "service_account" }
or
{ "web": { "private_key_id": "77bee9dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "private_key": "-----begin private key-----\nmiicdgi ... \u003d\u003d\n-----end private key-----\n", "client_email": "243714256753-g21p1xxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com", "client_id": "243714256753-g21p1xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", "type": "service_account" } }
in exchangecode()
googleauthorizationcodetokenrequest.execute()
throws
com.google.api.client.auth.oauth2.tokenresponseexception: 400 bad request { "error" : "invalid_request", "error_description" : "client_secret missing." }
1) shall know ?
2) what's use of jwt obtained on onuploadserverauthcode
?
Comments
Post a Comment