Solved

Google Drive APi does not Authenticate when using in Asp.net

Posted on 2015-01-05
3
733 Views
Last Modified: 2016-09-27
I am trying to use Google Drive API in my Asp.net web app.
I have hosted my on my local ISS and I had deleted the default web site from ISS and put my app in it.
So that I can use the URL "http://localhost"

I am trying to call this method, when I am running it, I am not getting any error the code runs but nothing happens not Consent Screen opens up for permission.

Not sure why this is not working.

public static async Task<bool> AuthenicateWithGoogle()
        {
            ClientSecrets secrets;

            using (var stream = new System.IO.FileStream(CommonFunction.GetFullPath("client_secrets.json",true),
               System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                secrets = GoogleClientSecrets.Load(stream).Secrets;
            }

            IDataStore credentialPersistanceStore = getPersistentCredentialStore();
            
            //UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets,
            //        SCOPES, getUserId(), CancellationToken.None, credentialPersistanceStore).Result;

            TokenResponse token;

            token = await credentialPersistanceStore.GetAsync<TokenResponse>("465464").ConfigureAwait(false);
            

            //var credential = new UserCredential(new GoogleAuthorizationCodeFlow(
            //    new GoogleAuthorizationCodeFlow.Initializer
            //    {
            //        ClientSecrets = secrets
            //    }),
            //    getUserId(), token);


            IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = secrets,
                Scopes = new[] { DriveService.Scope.Drive },
                DataStore = new FileDataStore(CommonFunction.GetFullPathForGoogleDataStoreFolder(), true)
            });


            UserCredential credential = new UserCredential(flow, getUserId(), token);


            googleService = new DriveService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = AppConstant.GoogleApplicationName
            });

            return true;
        }

Open in new window

0
Comment
Question by:yadavdep
3 Comments
 
LVL 79

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 40531891
//Scopes for use with the Google Drive API
string[] scopes = new string[] { DriveService.Scope.Drive,
                                 DriveService.Scope.DriveFile};
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = 
            GoogleWebAuthorizationBroker
                          .AuthorizeAsync(new ClientSecrets { ClientId = CLIENT_ID
                                                            , ClientSecret = CLIENT_SECRET }
                                          ,scopes

Open in new window


http://www.daimto.com/google-drive-api-c-download/#Authenticating_C_to_Google_Drive
                                          ,Environment.UserName
                                          ,CancellationToken.None
                                          ,new FileDataStore("Daimto.GoogleDrive.Auth.Store")
                                          ).Result;
0
 

Author Comment

by:yadavdep
ID: 40531917
David,

 The example you shared is a console application, here also everything is working on console application.
Not sure what is wrong on Web application
0
 

Expert Comment

by:Ilan Olkies
ID: 41818179
I've run in the same issue, what was the solutions you found?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn about cloud computing and its benefits for small business owners.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will demonstrate Google Calendar to monitor updates with top sites, such as Facebook, Google, Twitter, etc. with Marketing News. Each update of Google Calendar can be monitored, correlate dips and spikes in your website traffic, …
This Micro Tutorial will demonstrate common damaging and frequent mistakes I see in most analytic audits. Most of them are campaign tagging mistakes, so this video will break it down into simple steps.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now