Google云端硬盘Api将文件上传到错误的Google云端硬盘帐户

时间:2018-06-01 13:43:00

标签: c# asp.net-mvc asp.net-core google-drive-api

我已在项目中将文件上传到Google云端硬盘功能。 以下是我从Google下载的凭据(客户端密钥和客户端密钥)json文件。

{
"web": {
    "client_id": "cid",
    "project_id": "projectid",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "cse"
}
}

下面的代码显示了使用此代码在我的机器中生成的访问令牌json文件。

  using (var stream = new FileStream("drive.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = System.IO.Path.Combine(credPath, ".credentials/accesstoken.json");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                 Scopes,
                "a.b@gmail.com",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }
        //create Drive API service.
        Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "DriveProjectName",
        });

文件上传功能运行良好,但是当我将我的代码从一台PC复制到另一台PC并浏览我的项目代码时,如果在该PC上登录了任何其他gmail帐户(例如xz@gmail.com),文件上传到该帐户的驱动器而非我的Google云端硬盘。

为什么会这样?此问题是否与任何Google云端硬盘权限或定义范围有关?是否需要在Google云端硬盘或我的代码中完成的任何特定设置?我在我的代码中定义了以下范围

public static string[] Scopes =
{
     Google.Apis.Drive.v3.DriveService.Scope.Drive, 
     Google.Apis.Drive.v3.DriveService.Scope.DriveFile, 
     Google.Apis.Drive.v3.DriveService.Scope.DriveMetadata, 
     Google.Apis.Drive.v3.DriveService.Scope.DriveAppdata
};

有任何建议或想法吗?

0 个答案:

没有答案