Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.Util;
- using System.Diagnostics;
- using DotNetOpenAuth.Messaging;
- using DotNetOpenAuth.OAuth2;
- using Google.Apis.Authentication;
- using Google.Apis.Authentication.OAuth2;
- using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
- using Google.Apis.Util;
- using PrepHub.PrepHub;
- using System.Web.Services;
- using System.Threading;
- using Google.Apis;
- using Google.Apis.Drive.v2.Data;
- using Google.Apis.Drive.v2;
- using Google.Apis.Drive;
- namespace DriveExample
- {
- public partial class GDrive : System.Web.UI.Page
- {
- private static DriveService _service; // We don't need individual service instances for each client.
- private OAuth2Authenticator<WebServerClient> _authenticator;
- private IAuthorizationState _state;
- private IAuthorizationState AuthState
- {
- get
- {
- return _state ?? HttpContext.Current.Session["AUTH_STATE"] as IAuthorizationState;
- }
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (_service == null)
- {
- _service = new DriveService(_authenticator = CreateAuthenticator());
- }
- if (HttpContext.Current.Request["code"] != null)
- {
- _authenticator = CreateAuthenticator();
- _authenticator.LoadAccessToken();
- }
- var ni = _service.Files.List().Fetch();
- }
- private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
- {
- var provider = new WebServerClient(GoogleAuthenticationServer.Description);
- provider.ClientIdentifier = ClientCredentials.ClientID;
- provider.ClientSecret = ClientCredentials.ClientSecret;
- var authenticator =
- new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization) { NoCaching = true };
- return authenticator;
- }
- private IAuthorizationState GetAuthorization(WebServerClient client)
- {
- // If this user is already authenticated, then just return the auth state.
- IAuthorizationState state = AuthState;
- if (state != null)
- {
- return state;
- }
- // Check if an authorization request already is in progress.
- state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
- if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
- {
- // Store and return the credentials.
- HttpContext.Current.Session["AUTH_STATE"] = _state = state;
- return state;
- }
- string scope = DriveService.Scopes.Drive.GetStringValue();
- OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
- response.Send();
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment