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
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.
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;
}
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;
}