【问题标题】:Google account issue - Cannot write on the spreadsheetGoogle 帐户问题 - 无法在电子表格上书写
【发布时间】:2015-09-24 10:13:32
【问题描述】:

我正在开发一个必须在谷歌电子表格上书写的应用程序。 我的应用程序在两个月前 Google 更改为 Auth API 之前一直运行良好。 按照新流程的链接。

Google.GData.Client.GDataRequestException - Authentication suddenly fails in old code

我有一个谷歌账户 me@company.com,由我的老板账户 company@gmail.com 管理。

我想写一个名为“银行详细信息”的电子表格,该电子表格从我的老板帐户共享给我。(名称是“mycompany”)

我从我的帐户生成了一个 key.p12,它工作正常,我可以写。

现在奇怪的是当我自己创建电子表格时(所有者是“我”,我的应用程序找不到文件并返回错误消息。

我从我的老板帐户生成了一个 key.p12,但它仍然找不到我老板创建的电子表格,所以从他的角度来看,所有者是“我”。

总结:

  • 我找到了老板共享的电子表格,里面有我的钥匙,我可以在里面写字。
  • 我没有找到我用我的密钥创建的文件。
  • 我的应用找不到我老板用他的密钥创建的文件。

这是我的代码,但我认为问题不是来自这里。

 string keyFilePath;    // found in developer console
                string serviceAccountEmail;
                if (test == true) 
                {
                    keyFilePath = @"C:\keyTEST.p12";
                    serviceAccountEmail = "me@developer.gserviceaccount.com";// found in developer console
                }
                else
                {
                    keyFilePath = @"C:\key.p12";
                    serviceAccountEmail ="company@developer.gserviceaccount.com";
                }
                var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);

                ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) //create credential using certificate
                {
                    Scopes = new[] { "https://spreadsheets.google.com/feeds/" } //this scopr is for spreadsheets, check google scope FAQ for others
                }.FromCertificate(certificate));

                credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait(); //request token

                var requestFactory = new GDataRequestFactory("Some Name");
                requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", credential.Token.AccessToken));

                SpreadsheetsService myService = new SpreadsheetsService("Bank Details"); //create your old service
                myService.RequestFactory = requestFactory; //add new request factory to your old service
                myService.setUserCredentials(email, password);



                // Instantiate a SpreadsheetQuery object to retrieve spreadsheets.
                SpreadsheetQuery query = new SpreadsheetQuery();

                // Make a request to the API and get all spreadsheets.
                SpreadsheetFeed feed = myService.Query(query);

                SpreadsheetEntry fileEntry = feed.Entries.Cast<SpreadsheetEntry>().FirstOrDefault(entry => entry.Title.Text == spreadSheetName);
                SpreadsheetEntry spreadsheet = (SpreadsheetEntry)fileEntry;

                if (feed.Entries.Count == 0)
                {
                    Console.WriteLine("None");
                }

...

【问题讨论】:

  • 我认为您不需要服务帐户。改用 3legged oauth2。如果您使用服务帐户,则该文件是在服务帐户中创建的,而不是您或您老板的,因此您也需要将其共享给他们。
  • 感谢您的回答。但是我没有找到任何 3-legged Oauth2 的代码,只有 3-legged Oauth1 或 2Legged Oauth2。你有什么好的链接可以分享吗?
  • 所有官方样本均为 oaurh2
  • 我的两个电子表格终于都写好了(老板分享,我自己创建的),但是授权url给出的访问码,每次我都需要新建一个吗必须写在我的电子表格上?每次我想检索此代码时,是否需要打开一个网页(从我安装的应用程序)?我希望用户只需要识别一次,做与谷歌文档有关的事情,直到他关闭我的应用程序。
  • 没有。请参阅有关刷新和访问令牌的文档。您从仅第一次出现的刷新令牌中生成访问令牌。无需打开网页即可生成访问令牌。

标签: c# google-sheets


【解决方案1】:

您需要在工作表中分享电子邮件“company@developer.gserviceaccount.com”或“me@developer.gserviceaccount.com”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多