[英]Google Drive Api for installed application c#
有人可以展示如何使用谷歌驅動器api安裝應用程序的工作示例代碼? (access_type = offline)我找到了一些解釋,但無法進入工作流程。
謝謝
好吧,我至少使用的不是“安裝的應用程序”,而是“網絡應用程序”。 這些是要做的步驟:
轉到Google Developers Console並創建一個項目。
轉到API和Auth
在API上啟用Drive API和Drive 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&Save the Acess&Refresh令牌。
編碼:
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為你推薦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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.