有人可以展示如何使用google drive api安装应用程序的工作示例代码吗? (access_type =离线) 我找到了一些解释,但无法达到工作流程。
由于
答案 0 :(得分:2)
好的,我至少使用的不是“已安装的应用程序”,而是“网络应用程序”。 这些是要做的步骤:
转到Google Developers Console并创建一个项目。
转到API&验证
在API上启用Drive API&推动SDK。
4.在凭据上为Web应用程序创建新的客户端ID(创建您的同意屏幕,尽管我们不会使用它。)
在“创建客户端ID”窗口中,将网址“https://developers.google.com/oauthplayground”添加到AUTHORIZED REDIRECT URIS。
5.转到您在第4号添加的网址
6.单击右侧的齿轮并配置:
OAuth flow: Server-side
Access type: Offline
Use your own OAuth credentials: Tick
Then copy your Client ID & Secret from the console.
7.在左侧,选择Drive API - > https://www.googleapis.com/auth/drive,点击授权API
8.将打开一个新窗口,要求您接受Google OAuth ...单击“接受”。
9.单击代币的Exchange授权代码。
10.Copy&拯救进程和刷新令牌。
代码:
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;
}
答案 1 :(得分:1)
这是我的帮助班,我使用Kinda为你推荐Google Drive。请记住服务帐户不是您。仅仅因为您创建它并不意味着它可以访问您的Google云端硬盘帐户中的文件。服务帐户有自己的实体,有自己的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:\Users\LL\Downloads\Diamto Test Everything Project-e8bf61cc9963.p12");