c# - How to use Google Contacts API via Oauth2 authentication -


i have below code calendar entries using google calendar api (https://developers.google.com/google-apps/calendar/) uses oauth2. works well.

private ilist<string> scopes = new list<string>(); private calendarservice calendarservice;  private void initializecalendarservice() {         // add calendar specific scope scopes list         scopes.add(calendarservice.scopes.calendar.getstringvalue());          // display header , initialize sample         commandline.enableexceptionhandling();         commandline.displaygooglesampleheader("google.api.calendar.v3 sample");          // create authenticator         //fullclientcredentials credentials = promptingclientcredentials.ensurefullclientcredentials();         var provider = new nativeapplicationclient(googleauthenticationserver.description);          fullclientcredentials credentials = new fullclientcredentials();         credentials.clientid = "xyz.apps.googleusercontent.com";         credentials.clientsecret = "xyz";         credentials.apikey = "xyz";          provider.clientidentifier = credentials.clientid;         provider.clientsecret = credentials.clientsecret;         oauth2authenticator<nativeapplicationclient> auth = new oauth2authenticator<nativeapplicationclient>(provider, getauthorization);          // create calendar service using initializer instance         baseclientservice.initializer initializer = new baseclientservice.initializer();         initializer.authenticator = auth;         calendarservice = new calendarservice(initializer);          calendarlist list = calendarservice.calendarlist.list().execute();         // list .. list  }   public iauthorizationstate getauthorization(nativeapplicationclient client) {         // should use more secure way of storing key here         // .net applications can disassembled using reflection tool.         const string storage = "google.samples.dotnet.calendar";         const string key = "s0mekey";          // check if there cached refresh token available.         iauthorizationstate state = authorizationmgr.getcachedrefreshtoken(storage, key);         if ((state != null))         {             try             {                 client.refreshtoken(state);                 return state;                 // done             }             catch (dotnetopenauth.messaging.protocolexception ex)             {                 commandline.writeerror("using existing refresh token failed: " + ex.message);                 commandline.writeline();             }         }          // retrieve authorization user         string[] array = new string[scopes.count];         scopes.copyto(array,0);         state = authorizationmgr.requestnativeauthorization(client, array);         authorizationmgr.setcachedrefreshtoken(storage, key, state);         return state; }  

how can use similar oauth2authenticator fetch contacts?

i able fetch contacts using below code, not password-less, need working using oath2. example below uses gdata contacts api v2. can see can pass through oauth2authenticator im not sure how correctly (i cant see valid examples in c# on google site) , fetch access code based on user selecting. cant see how use oauth2authenticator contacts api v3 (https://developers.google.com/google-apps/contacts/v3/)

requestsettings rslogininfo = new requestsettings("", email,pwd); rslogininfo.autopaging = true; contactsrequest crequest = new contactsrequest(rslogininfo);  // fetch contacts list feed<contact> feedcontacts = crequest.getcontacts(); foreach (contact gmailaddresses in feedcontacts.entries) {         // looping read  email addresses         foreach (email emailid in gmailaddresses.emails)         {            lstcontacts.add(emailid.address);         } } 

i ended doing fetching access code having browser control read document title value when user selects google account , grants access.

eg:

to generate url

redirecturi = "urn:ietf:wg:oauth:2.0:oob"  oauth2parameters parameters = new oauth2parameters() {     clientid = clientid,     clientsecret = clientsecret,     redirecturi = redirecturi,     scope = requiredscope };   // request authorization user (by opening browser window): string url = oauthutil.createoauth2authorizationurl(parameters); var loginuri = new uri(url);  // form has browser control googleloginform form = new googleloginform(loginuri, redirecturi); var dr = form.showdialog();  if (dr == system.windows.forms.dialogresult.ok) {     parameters.accesscode = form.oauthverifiertoken; } 

then in googleloginform : have browser control , registered browsercontrol_navigated event , below. documenttitle contains accesscode used generate token.

private void googleloginform_load(object sender, eventargs e) {    wbgooglelogin.url = _loginuri; }  private void wbgooglelogin_navigated(object sender, webbrowsernavigatedeventargs e) {     string fullpath = e.url.tostring();     webbrowser control = sender webbrowser;     if (control != null &&  !string.isnullorempty(control.documenttitle) && control.documenttitle.contains("success code"))     {        _oauthverifiertoken = control.documenttitle.replace("success code=","");        dialogresult = dialogresult.ok;     } } 

this way can done in same piece of code, without having write complicated callback service of sort read access token our system.

not sure why calendar api has built in, , contacts api doesn't.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -