/// <summary> /// Associates a Google Drive account /// </summary> /// <param name="uid"></param> /// <param name="oauth_token"></param> /// <returns></returns> public ActionResult AssociateGoogleDrive(string code) { // this url needs to be absolute, because it's going to Google var callbackUrl = string.Format( "{0}://{1}{2}", this.Url.RequestContext.HttpContext.Request.Url.Scheme, this.Url.RequestContext.HttpContext.Request.Url.Host, this.Url.Action("AssociateGoogleDrive", "GoogleDriveCallback", new { area = (string)null })); var authorizationState = GoogleApiHelper.ExchangeCode(code, null, callbackUrl); var client = new WebClient(); client.Headers["Authorization"] = "OAuth " + authorizationState.AccessToken; var resultBytes = client.DownloadData("https://www.googleapis.com/oauth2/v2/userinfo"); var resultString = Encoding.UTF8.GetString(resultBytes); var result = new JavaScriptSerializer().Deserialize <AccountInfoJsonResult>(resultString); this.db.GoogleUserAccountInfo.AddObject( new GoogleUserAccoutInfo() { AuthenticationCode = code, Email = result.email, Name = result.name, PracticeId = this.DbPractice.Id, PersonId = this.DbUser.Id, RefreshToken = authorizationState.RefreshToken }); this.db.Notifications.AddObject(new Notification { CreatedOn = this.GetUtcNow(), PracticeId = this.DbPractice.Id, UserToId = this.DbUser.Id, Type = NotificationConstants.GOOGLE_DRIVE_ASSOCIATED_NOTIFICATION_TYPE }); this.db.SaveChanges(); return(this.RedirectToAction("Index")); }