google oauth - Java - GMail API - Authorization code -


i trying code below. console prints out url (after pasting in address bar on browser) sends me google's user-consent page , asks permission access account. redirects me html page - far good.

now, i'm not sure if receive token or authorization code. obtain from? , have send http rest call web application go along gmail api request, or can via java?

public class people {   public void setup() throws ioexception {         httptransport httptransport = new nethttptransport();         jacksonfactory jsonfactory = new jacksonfactory();          string clientid = "client_id";         string clientsecret = "secret";          string redirecturl = "http://localhost:8080/testinggmailmail/webapps/login.html";         string scope = "https://www.googleapis.com/auth/contacts.readonly";           string authorizationurl = new googlebrowserclientrequesturl(clientid,redirecturl,arrays.aslist(scope)).build();           // point or redirect user authorizationurl.         system.out.println("go following link in browser:");         system.out.println(authorizationurl);         } } 

yes, @dalmto correct in pointing out client library 1 should handling this.

a better design store user credentials application in directory.

private static final java.io.file data_store_dir = new java.io.file(     system.getproperty("user.home"), ".store/mail_credentials"); 

now, make global instance filedatastorefactory.

private static filedatastorefactory data_store_factory; 

instantiate data_store_factory before getting credentials preferably in static block.

data_store_factory = new filedatastorefactory(data_store_dir); 

download , store client_secret.json google developer console. use following method credentials:

public static credential authorize() throws ioexception {     // load client secrets.     inputstream in             = gmailquickstart.class.getresourceasstream("/client_secrets.json");     googleclientsecrets clientsecrets             = googleclientsecrets.load(json_factory, new inputstreamreader(in));      // build flow , trigger user authorization request.     googleauthorizationcodeflow flow             = new googleauthorizationcodeflow.builder(                     http_transport, json_factory, clientsecrets, scopes)             .setdatastorefactory(data_store_factory)             .setaccesstype("offline")             .build();     credential credential = new authorizationcodeinstalledapp(             flow, new localserverreceiver()).authorize("user");     system.out.println(             "credentials saved " + data_store_dir.getabsolutepath());     return credential; } 

whenever above method called, looks storedcredential in path provided data_store_dir. if found, code executes is. if not, browser open ask login , authorize app. credentials generated such stored in data_store_dir location. long storedcredential present, app won't ask permission. general purpose design can used other google apis.


Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -