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