有人能展示一个如何为已安装的应用程序使用谷歌驱动api的工作示例代码吗?(access_type=脱机)我找到了一些解释,但无法找到工作流程。
感谢
好的,我至少使用的不是"已安装的应用程序",而是"web应用程序"。以下是要做的步骤:
-
转到谷歌开发者控制台并创建一个项目。
-
转到API&Auth
-
在API上启用驱动器API&驱动器SDK。
4.在凭据上为Web应用程序创建新的客户端ID(创建您的同意屏幕,尽管我们不会使用它。)
在"创建客户端ID"窗口中,添加url"https://developers.google.com/oauthplayground"至授权REDIRECT URIS。
5.转到你在4号上添加的url
6.点击右侧的档位并配置:
OAuth flow: Server-side
Access type: Offline
Use your own OAuth credentials: Tick
Then copy your Client ID & Secret from the console.
7.在左侧,选择驱动API->https://www.googleapis.com/auth/drive,单击授权API
8.将打开一个新窗口,要求您接受Google OAuth。。。单击"接受"。
9.单击代币的Exchange授权代码。
10.复制&保存王牌&刷新令牌。
代码:
private static DriveService CreateServie(string applicationName)
{
var tokenResponse = new TokenResponse
{
AccessToken = yourAccessToken,
RefreshToken = yourRefreshToken,
};
var apiCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = yourClientID,
ClientSecret = yourClientSecret
},
Scopes = new[] { DriveService.Scope.Drive },
DataStore = new FileDataStore(applicationName)
});
var credential = new UserCredential(apiCodeFlow, yourEMail, tokenResponse);
var service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = applicationName
});
return service;
}
这是我的助手类,我用Kinda为你在谷歌硬盘上推了它。请记住,服务帐户不是您。仅仅因为你创建了它并不意味着它可以访问你的谷歌硬盘帐户上的文件。服务帐户有自己的实体,有自己的sudu用户。这创建了基本的驱动器服务,您可以使用它来遵循我创建的使用普通Oauth2 的其他教程
/// <summary>
/// Authenticating to Google using a Service account
/// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
/// </summary>
/// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param>
/// <param name="keyFilePath">Location of the Service account key file downloaded from Google Developer console https://console.developers.google.com</param>
/// <returns></returns>
public static DriveService AuthenticateServiceAccount(string serviceAccountEmail, string keyFilePath)
{
// check the file exists
if (!File.Exists(keyFilePath))
{
Console.WriteLine("An Error occurred - Key file does not exist");
return null;
}
string[] scopes = new string[] { DriveService.Scope.Drive}; // View analytics data
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
try
{
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
}
你这样称呼它
var x = AuthenticationHelper.AuthenticateServiceAccount("46123799103-6v9cj8jbub068jgmss54m9gkuk4q2qu8@developer.gserviceaccount.com",@"C:UsersLLDownloadsDiamto Test Everything Project-e8bf61cc9963.p12");
教程:Google Drive API与服务帐户C#