How to read credentials from user json file for Google Accounts

Jason Phang

Well-known member
Joined
Aug 13, 2019
Messages
46
Programming Experience
Beginner
I am able to read the google credentials from my json file if I were to provide a path for it. My question would be how I can do it if I want like everytime I run my app, the sign in page to the Google Auth 2.0 will be loaded? I have tried several ways like specifying a path such as
Special path for google auth 2.0:
 string savePath = Path.Combine(appDataSavePath , Path.GetFileName(clientSecretPath));
            if (System.IO.File.Exists(savePath))
            {
                try
                {
                    using (var stream = new FileStream(savePath, FileMode.Open, FileAccess.Read))
                    {
                        string  credPath = Path.Combine(appDataSavePath, ".credentials");

                        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                            GoogleClientSecrets.Load(stream).Secrets,
                            Scopes,
                            "Drive-"+userName,
                            CancellationToken.None,
                            new FileDataStore(credPath, true)).Result;
                    }
                    return true;

                }
However, this des not work due to the message that daily limit for authentication used exceeded. This is what I have for now but it only is applicable for my own google account as I specify my own path to the json file.
Own Json path:
  public static DriveService GetService()
        {
            //get Credentials from client_secret.json file
            UserCredential credential;
            using (var stream = new FileStream(@"D:client_secret.json", FileMode.Open, FileAccess.Read))
            {
                String FolderPath = @"D:\";
                String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(FilePath, true)).Result;
            }
            
            //create Drive API service.
            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "GoogleDriveRestAPI-v3",
            });
            return service;
        }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
I'm sorry. I read your question 10 times, and I don't understand how your title (about how to read user credentials from JSON file) is related to the problem that you are decribing (daily limit for authentication used exceeded). To me the problems are orthogonal to each other. Either you are successfully reading the credentials or not. Either you have tried logging on too many times or not.

Any way, you are correctly reading the credentials with your:
C#:
using (var stream = new FileStream(savePath, FileMode.Open, FileAccess.Read))
where savePath happens to be "D:client_secret.json" or the result of your verified file exists Path.Combine(appDataSavePath , Path.GetFileName(clientSecretPath)).

Can you try rephrasing your question?
 
Last edited:

Jason Phang

Well-known member
Joined
Aug 13, 2019
Messages
46
Programming Experience
Beginner
Okay, perhaps I will try to rephrase my question. As of now, my code is only applicable to my own path which means when i specify that the json file is in the d path, then when i launch the app, it will be straight away connected to my google drive. For instance,
My own path for the json file:
  using (var stream = new FileStream(@"D:client_secret.json", FileMode.Open, FileAccess.Read))
            {
                String FolderPath = @"D:\";
                String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(FilePath, true)).Result;
            }
As i have already specified the path, hence, it will be running from my own path of the json file in the d drive. My question is how do I do it in a way that instead of me needing to specify the path, it will get the json file from the user path and run it. Means instead of me specifiying the path, the program will get the user crendetials of the json file for that user and it will pop up the Google Auth 2.0 page so that the user can select their google account instead of automatically now linking straight to my account as the path is specified to my json file.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,917
Location
Sydney, Australia
Programming Experience
10+
What exactly do you mean by "the user path"? Do you mean a path selected by the user via your app or a standard relative path under the current Windows user's profile folder or something else?
 

Jason Phang

Well-known member
Joined
Aug 13, 2019
Messages
46
Programming Experience
Beginner
For example, this code here
C#:
 using (var stream = new FileStream(@"D:client_secret.json", FileMode.Open, FileAccess.Read))
            {
                String FolderPath = @"D:\";
                String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");

specifies the path to the json file which is located on my computer. Hence, when i run the app, it will redirect me straight away to my google account. My question is how do I instead of setting a path where I already know the location of my json credentials file, i can retrieve the users credential json file. So instead of like a static path, it get the path of the json file from the user whenever the google auth 2.0 occurs. I have tried this so far but
C#:
  using (var stream = new FileStream("Secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/Secret_Credentials.json");
it says
System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not find file 'C:\Program Files (x86)\IIS Express\Secret.json'.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,917
Location
Sydney, Australia
Programming Experience
10+
If you want the user to specify the path then prompt the user for the path. It doesn't matter what the path is for. Getting a file path from the user is the same regardless, so finding information on how to do it is easy. Once you have the path you can store it wherever is appropriate and then use it without prompting every time.
 
Top Bottom