2

I m developing an App which has to write on google spreadsheet. My application used to work fine until Google changed is Auth API two months ago. Following the link with the new process.

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

I have a google account me@company.com which is managed by my boss account company@gmail.com.

I want to write in a spreadsheet named "Bank Details" shared to me from my boss account.(the onwer is "mycompany")

I generated a key.p12 from my account and it works fine, I can write.

Now the strange things is when i create a spreadsheet by myself (the owner is "me", my app doesn t find the file and return an error message.

I generated a key.p12 from my boss account and it still doesn't find the spreadsheet created by my boss, so from his point of view the owner is "me".

In conclusion:

  • I find the spreadsheet shared by my boss with my key and I can write in it.
  • I don't find the file created by me with my key.
  • My app doesn't find the file created by my boss with his key.

Here my code but i don't think the problem comes from here.

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

...

Community
  • 1
  • 1
Cantinou
  • 126
  • 5
  • 21
  • i dont think you need service accounts. use 3legged oauth2 instead. if you use service accounts the file is created in the service account, not yours or your boss thus you would need to share it to them as well. – Zig Mandel Jul 07 '15 at 13:59
  • Thank you for your answer. But i don t find any piece of code for a 3-legged Oauth2, only 3-legged Oauth1 or 2Legged Oauth2. Do you have any good link to share? – Cantinou Jul 08 '15 at 09:12
  • all offficial samples are oaurh2 – Zig Mandel Jul 08 '15 at 12:19
  • I've finally written in both of my spreadsheet (shared by my boss and created by me), but the access code give by the authorization url, do I need to create a new one each time I have to write on my spreadsheet? And do I need to open a web page (from my installed application) each time I want to retrieve this code? I would like the user needs to identify only one time, do what has to do with the google docs until he closes my app. – Cantinou Jul 09 '15 at 02:54
  • no. see the docs about refresh and access tokens. you produce an access token from a refresh token which only comes the first time. no need to open a webpage to generate the access token. – Zig Mandel Jul 09 '15 at 02:56
  • ok, i check that. Thanks – Cantinou Jul 09 '15 at 02:58

1 Answers1

0

You need share in your sheet the e-mail "company@developer.gserviceaccount.com" or "me@developer.gserviceaccount.com".

Junior
  • 199
  • 1
  • 9