如果要为其提供路径,则可以从json文件中读取google凭据。我的问题是,如果希望每次运行我的应用程序时都可以加载Google Auth 2.0的登录页面,该怎么办?我尝试了几种方法,例如指定诸如
但是,由于消息已超过每日使用的验证限制,因此此操作无效。这是我现在拥有的,但是仅当我指定自己的json文件路径时,它才适用于我自己的Google帐户。
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,
能够cellationToken.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",
能够cellationToken.None,
new FileDataStore(FilePath, true)).Result;
}
//create Drive API service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
}