Guest User

Untitled

a guest
Jan 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Web.Util;
  8. using System.Diagnostics;
  9. using DotNetOpenAuth.Messaging;
  10. using DotNetOpenAuth.OAuth2;
  11. using Google.Apis.Authentication;
  12. using Google.Apis.Authentication.OAuth2;
  13. using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
  14. using Google.Apis.Util;
  15. using PrepHub.PrepHub;
  16. using System.Web.Services;
  17. using System.Threading;
  18. using Google.Apis;
  19. using Google.Apis.Drive.v2.Data;
  20. using Google.Apis.Drive.v2;
  21. using Google.Apis.Drive;
  22.  
  23.  
  24. namespace DriveExample
  25. {
  26. public partial class GDrive : System.Web.UI.Page
  27. {
  28. private static DriveService _service; // We don't need individual service instances for each client.
  29. private OAuth2Authenticator<WebServerClient> _authenticator;
  30. private IAuthorizationState _state;
  31.  
  32. private IAuthorizationState AuthState
  33. {
  34. get
  35. {
  36. return _state ?? HttpContext.Current.Session["AUTH_STATE"] as IAuthorizationState;
  37. }
  38. }
  39.  
  40. protected void Page_Load(object sender, EventArgs e)
  41. {
  42.  
  43. if (_service == null)
  44. {
  45. _service = new DriveService(_authenticator = CreateAuthenticator());
  46. }
  47.  
  48.  
  49. if (HttpContext.Current.Request["code"] != null)
  50. {
  51. _authenticator = CreateAuthenticator();
  52. _authenticator.LoadAccessToken();
  53. }
  54.  
  55. var ni = _service.Files.List().Fetch();
  56.  
  57. }
  58.  
  59.  
  60. private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
  61. {
  62.  
  63. var provider = new WebServerClient(GoogleAuthenticationServer.Description);
  64. provider.ClientIdentifier = ClientCredentials.ClientID;
  65. provider.ClientSecret = ClientCredentials.ClientSecret;
  66. var authenticator =
  67. new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization) { NoCaching = true };
  68. return authenticator;
  69. }
  70.  
  71. private IAuthorizationState GetAuthorization(WebServerClient client)
  72. {
  73. // If this user is already authenticated, then just return the auth state.
  74. IAuthorizationState state = AuthState;
  75. if (state != null)
  76. {
  77. return state;
  78. }
  79. // Check if an authorization request already is in progress.
  80. state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
  81. if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
  82. {
  83. // Store and return the credentials.
  84. HttpContext.Current.Session["AUTH_STATE"] = _state = state;
  85. return state;
  86. }
  87.  
  88. string scope = DriveService.Scopes.Drive.GetStringValue();
  89. OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
  90. response.Send();
  91. return null;
  92. }
  93.  
  94. }
  95. }
Add Comment
Please, Sign In to add comment