After a bit 'of headaches I was able to understand where wrong, as I said in the comments I generate the certificate by:
The first step that I did is create the API for Google Calendar, later I clicked on "create credentials" and selected "Service account", choosing the API that I've created before and the key type as json.
this certificate is different against the key the the X509Certificate2
waiting, so the correct step to do is:
1. Click on Manage Service Account on the Developer Console, this workding is on the right of the credentials tab, just a little 'over to your projects list.
2. A new window appears, you need to click on the three dots next to the project you want to create the key. (The three dots are on the right).
3. A pop-up menu appear, and then, you need to click Create Key
.
4. Chose the P12 format, and then click on Create.
5. Save the file downloaded on a folder and link them in your code, in particular:
var certificate = new X509Certificate2(@"C:\key.p12", "notasecret", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
note that key.p12
is the name of the file of the certificate, and notasecret
is the default password that appear just a bit later of the step 4, in other word is the password associated to the certificate.
So my code seems to find the file and read it correctly without display any errors.
Thanks anyway to MegaTron that have aroused in me the doubt that the certificate was somehow not right.