2

I'm trying to work with Google Sheets API with Xamarin Forms but I'm facing with problems reading the client_secret.json file that is used to authenticate with Google as it does when programming with .NET. When executing the following lines:

using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        { ... }

I'm getting an error showing that can't find the file client_secret.json. I'm putting the property for that file in my solution making it to copy to directory with the "Always Copy" option, but no luck with this. (I'm using Visual Studio 2017).

Can anybody help me with this issue? Thank you.

Updated: The code I use is the following (any help would be appreciated):

        AssetManager assets = Android.App.Application.Context.Assets;
        var path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), @"client_secret.json");
        using (var asset = assets.Open("client_secret.json"))
        using (var dest = System.IO.File.Create(path))
            asset.CopyTo(dest);

        using (var stream = assets.Open(@"client_secret.json"))
        {
            var secrets = GoogleClientSecrets.Load(stream).Secrets;

            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, "sheets.googleapis.com-dotnet-quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
         }
Levimatt
  • 453
  • 1
  • 11
  • 28

2 Answers2

0

I had trouble with the client_secret.json file too. The instructions for where it belonged didn't work for me.

So I put a copy in my home directory's .credentials directory (/user/.credentials ), and I put a copy in the directory of the executing application - and one of the two worked. I would try placing client_secret.json in different directories until the application finds it.

NSchorr
  • 875
  • 10
  • 13
  • could you please explain more .. what i need to do to avoid this issue – Mohamad Mahmoud Darwish Feb 01 '18 at 10:43
  • The app needs to be able to find it. But perhaps in your case it's more than that. Have you seen this page? https://android-developers.googleblog.com/2016/02/using-credentials-between-your-server.html – NSchorr Feb 02 '18 at 23:55
0

you need to do the following:

1-put the client_secret.json In Assest.

2-try to read the json from Assest

 using (var stream = this.Assets.Open(@"client_secret.json"))
    {
        var secrets = GoogleClientSecrets.Load(stream).Secrets;
    }
Mohamad Mahmoud Darwish
  • 3,865
  • 9
  • 51
  • 76