c# google Contact api Insert contact without permission -


c# google contact api, want register users in google contact list. issue each time when save contact google asks permission. using below code in asp.net website want use web.api.

protected void page_load(object sender, eventargs e) {     if (request.querystring["code"] != null)         getaccesstoken(); }  protected void googlebutton_click(object sender, eventargs e) {     /*https://developers.google.com/google-apps/contacts/v3/       https://developers.google.com/accounts/docs/oauth2webserver       https://developers.google.com/oauthplayground/     */     string clientid = "yourclientid";     string redirecturl = "http://www.yogihosting.com/tutorialcode/googlecontactapi/google-contact-api.aspx";     response.redirect("https://accounts.google.com/o/oauth2/auth?redirect_uri=" + redirecturl + "&response_type=code&client_id=" + clientid + "&scope=https://www.google.com/m8/feeds/&approval_prompt=force&access_type=offline"); }  public void getaccesstoken() {     string code = request.querystring["code"];     string google_client_id = "yourclientid";     string google_client_sceret = "yourclientsecret";     string google_redirect_url = "http://www.yogihosting.com/tutorialcode/googlecontactapi/google-contact-api.aspx";      /*get access token , refresh token*/     httpwebrequest webrequest = (httpwebrequest)webrequest.create("https://accounts.google.com/o/oauth2/token");     webrequest.method = "post";     string parameters = "code=" + code + "&client_id=" + google_client_id + "&client_secret=" + google_client_sceret + "&redirect_uri=" + google_redirect_url + "&grant_type=authorization_code";     byte[] bytearray = encoding.utf8.getbytes(parameters);     webrequest.contenttype = "application/x-www-form-urlencoded";     webrequest.contentlength = bytearray.length;     stream poststream = webrequest.getrequeststream();     // add post data web request     poststream.write(bytearray, 0, bytearray.length);     poststream.close();     webresponse response = webrequest.getresponse();     poststream = response.getresponsestream();     streamreader reader = new streamreader(poststream);     string responsefromserver = reader.readtoend();     googleplusaccesstoken serstatus = jsonconvert.deserializeobject<googleplusaccesstoken>(responsefromserver);     /*end*/     getcontacts(serstatus); }  public void getcontacts(googleplusaccesstoken serstatus) {     string google_client_id = "yourclientid";     string google_client_sceret = "yourclientsecret";     /*get google contacts access token , refresh token*/     string refreshtoken = serstatus.refresh_token;     string accesstoken = serstatus.access_token;     string scopes = "https://www.google.com/m8/feeds/contacts/default/full/";     oauth2parameters oauthparameters = new oauth2parameters()     {         clientid = google_client_id,         clientsecret = google_client_sceret,         redirecturi = "http://www.yogihosting.com/tutorialcode/googlecontactapi/google-contact-api.aspx",         scope = scopes,         accesstoken = accesstoken,         refreshtoken = refreshtoken     };       requestsettings settings = new requestsettings("<var>your_application_name</var>", oauthparameters);     contactsrequest cr = new contactsrequest(settings);     contactsquery query = new contactsquery(contactsquery.createcontactsuri("default"));     query.numbertoretrieve = 5000;     feed<contact> feed = cr.get<contact>(query);      stringbuilder sb = new stringbuilder();     int = 1;     foreach (contact entry in feed.entries)     {         foreach (email email in entry.emails)         {             sb.append(i + "&nbsp;").append(email.address).append("<br/>");             i++;         }     }     contact newentry = new contact();         // set contact's name.         newentry.name = new name()         {             fullname = "elizabeth bennet",             givenname = "elizabeth",             familyname = "bennet",         };         newentry.content = "notes";         // set contact's e-mail addresses.         newentry.emails.add(new email()         {             primary = true,             rel = contactsrelationships.ishome,             address = "liz@gmail.com"         });         newentry.emails.add(new email()         {             rel = contactsrelationships.iswork,             address = "liz@example.com"         });         // set contact's phone numbers.         newentry.phonenumbers.add(new phonenumber()         {             primary = true,             rel = contactsrelationships.iswork,             value = "(206)555-1212",         });         newentry.phonenumbers.add(new phonenumber()         {             rel = contactsrelationships.ishome,             value = "(206)555-1213",         });         // set contact's im information.         newentry.ims.add(new imaddress()         {             primary = true,             rel = contactsrelationships.ishome,             protocol = contactsprotocols.isgoogletalk,         });         // set contact's postal address.         newentry.postaladdresses.add(new structuredpostaladdress()         {             rel = contactsrelationships.iswork,             primary = true,             street = "1600 amphitheatre pkwy",             city = "mountain view",             region = "ca",             postcode = "94043",             country = "united states",             formattedaddress = "1600 amphitheatre pkwy mountain view",         });         // insert contact.         uri feeduri = new uri(contactsquery.createcontactsuri("default"));         contact createdentry = cr.insert(feeduri, newentry);      /*end*/     datadiv.innerhtml = sb.tostring(); } 

is there way can save users details in google permission, ok if google asks permission first time, not time.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -