Google 驱动器 Api 用于已安装的应用程序 c#

Google Drive Api for installed application c#

有人可以展示如何使用 google 驱动器 api 来安装应用程序的工作示例代码吗? ( access_type=离线) 我找到了一些解释,但无法进入工作流程。

谢谢

这是我的帮手 class 我用 Kinda tweeked 为您制作 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");

教程:Google Drive API with Service Account C#

好吧,我至少用的不是'installed application',而是'web application'。 这些是要执行的步骤:

  1. 转到 Google Developers Console 并创建一个项目。

  2. 转到 APIs & Auth

  3. 在 API 秒启用 Drive API 和 Drive SDK。

4.On 凭据为 Web 应用程序创建新的客户端 ID(创建您的同意屏幕,尽管我们不会使用它。)

在创建客户端 ID window 中,将 url“https://developers.google.com/oauthplayground”添加到 AUTHORIZED REDIRECT URIS。

5.Go 到您在数字 4

上添加的 url

6.Click右边的档位和配置:

OAuth flow: Server-side

Access type: Offline

Use your own OAuth credentials: Tick

Then copy your Client ID & Secret from the console.

7.On左侧,选择Drive API -> https://www.googleapis.com/auth/drive,点击Authorize APIs

8.A 新 window 将打开,要求您接受 Google OAuth...单击接受。

9.Click 兑换代币授权码。

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