protected void Page_Load(object sender, EventArgs e)
        {
            GoogleAuthorizationCodeFlow flow;
            var assembly = Assembly.GetExecutingAssembly();
            using (var stream = assembly.GetManifestResourceStream("Tasks.ASP.NET.SimpleOAuth2.client_secrets.json"))
            {
                flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    DataStore = new FileDataStore("Tasks.ASP.NET.Sample.Store"),
                    ClientSecretsStream = stream,
                    Scopes = new[] { TasksService.Scope.TasksReadonly }
                });
            }

            var uri = Request.Url.ToString();
            var code = Request["code"];
            if (code != null)
            {
                var token = flow.ExchangeCodeForTokenAsync(UserId, code,
                    uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;

                // Extract the right state.
                var oauthState = AuthWebUtility.ExtracRedirectFromState(
                    flow.DataStore, UserId, Request["state"]).Result;
                Response.Redirect(oauthState);
            }
            else
            {
                var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId,
                    CancellationToken.None).Result;
                if (result.RedirectUri != null)
                {
                    // Redirect the user to the authorization server.
                    Response.Redirect(result.RedirectUri);
                }
                else
                {
                    // The data store contains the user credential, so the user has been already authenticated.
                    service = new TasksService(new BaseClientService.Initializer
                    {
                        ApplicationName = "Tasks API Sample",
                        HttpClientInitializer = result.Credential
                    });
                }
            }
        }
        public object Authorize(HttpRequest Request, string accountName)
        {
            try
            {
                var uri = Request.Url.ToString();
                var code = Request["code"];
                var error = Request["error"];

                if (!string.IsNullOrEmpty(error))
                    if (error == "access_denied")
                        return new UserRejectException();
                    else
                        return new UnknownException(error);

                BloggerService = null;

                if (code != null)
                {
                    string redirectUri = uri.Substring(0, uri.IndexOf("?"));
                    var token = CodeFlow.ExchangeCodeForTokenAsync(accountName, code, redirectUri, CancellationToken.None).Result;
                    string state = Request["state"];
                    var result = AuthWebUtility.ExtracRedirectFromState(CodeFlow.DataStore, accountName, state);
                    return result;
                }
                else
                {
                    string redirectUri = uri;
                    string state = "ostate_";// Guid.NewGuid().ToString("N");
                    var result = new AuthorizationCodeWebApp(CodeFlow, redirectUri, state).AuthorizeAsync(accountName, CancellationToken.None).Result;
                    if (result.RedirectUri != null)
                    {
                        return result;
                    }
                    else
                    {
                        BloggerService = new BloggerService(new BaseClientService.Initializer()
                        {
                            HttpClientInitializer = result.Credential,
                            ApplicationName = APPLICATION_NAME
                        });
                        // alright
                        return "OK";
                    }
                }
            }
            catch (Exception ex)
            {
                return ex;
            }
        }
Beispiel #3
0
        public async Task<string> GetFileUri(AuthorizationCodeWebApp.AuthResult result)
        {
            var driveService = new DriveService(new BaseClientService.Initializer
            {
                HttpClientInitializer = result.Credential,
                ApplicationName = "ASP.NET Google APIs MVC Sample"
            });

            var listReq = driveService.Files.List();
            listReq.Fields = "items/title,items/id,items/createdDate,items/downloadUrl,items/exportLinks";

            var list = await listReq.ExecuteAsync();

            var timeReport = list.Items.FirstOrDefault(x => x.ExportLinks != null && x.Title.Contains("Time Report"));
            if (timeReport == null)
            {
                throw new Exception(
                    "You dont have Time Report./n Reason 1)You dont have xslx doc that contains Time report in name");
            }
            var downloaduri = timeReport.ExportLinks.FirstOrDefault(x => x.Value.Contains("xlsx")).Value;
            return downloaduri;
        }
Beispiel #4
0
        public async Task<List<FileModel>> GetFileModels(AuthorizationCodeWebApp.AuthResult result)
        {
            var driveService = new DriveService(new BaseClientService.Initializer
            {
                HttpClientInitializer = result.Credential,
                ApplicationName = "ASP.NET Google APIs MVC Sample"
            });

            var listReq = driveService.Files.List();
            listReq.Fields = "items/title,items/id,items/createdDate,items/downloadUrl,items/exportLinks";

            var list = await listReq.ExecuteAsync();

            var items = (list.Items.Where(x => x.ExportLinks != null).Select(file => new FileModel
            {
                Title = file.Title,
                Id = file.Id,
                CreatedDate = file.CreatedDate,
                ExprotList = file.ExportLinks.Select(x => x.Value).ToList(),
                DownloadUrl = file.DownloadUrl
            })).OrderBy(f => f.Title).ToList();
            return items;
        }
 public AuthResultAdapter(AuthorizationCodeWebApp.AuthResult result)
 {
     _result = result;
 }