【问题标题】:Xamarin Forms IOS Google Sheets API: Can't find client_secret.json fileXamarin Forms IOS Google Sheets API:找不到client_secret.json文件
【发布时间】:2021-08-03 05:05:52
【问题描述】:

我正在尝试在我的 xamarin forms ios 项目中读取 google 表格数据。我遵循了本教程:https://www.youtube.com/watch?v=afTiNU6EoA8&t=325s 无济于事。

我的程序说找不到 client_secret.json 文件,我也找不到解决问题的方法。

it gives me this exception error

这是我的代码:

using System;
using Google.Apis.Sheets.v4;
using Google.Apis.Auth.OAuth2;
using System.IO;
using Google.Apis.Services;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace CUapp
{

public class GoogleSheets
{
    static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };
    static readonly string SpreadsheetId = "1Me8q2o54xphoO_L1l-aK6_qzwIX2AAmmZzCCxvmGbl8";
    static readonly string ApplicationName = "CUapp";
    static readonly string sheet = "Sheet1";
    static SheetsService service;



    public GoogleSheets()
    {
        GoogleCredential credential;
        using (
            var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        //var stream = Assembly.GetManifestResourceStream("client_secret.json"))
        {
            credential = GoogleCredential.FromStream(stream)
                .CreateScoped(Scopes);
        }

        // Create Google Sheets API service.
        service = new SheetsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential, ApplicationName = ApplicationName
            
        });
        ReadEntries();
    }

    static public void ReadEntries()
    {
        var range = $"{sheet}!B1:B30";
        SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(SpreadsheetId, range);
        request.MajorDimension = SpreadsheetsResource.ValuesResource.GetRequest.MajorDimensionEnum.COLUMNS;

        var response = request.Execute();
        var values = response.Values;

        if (values != null && values.Count > 0)
        {
            foreach (var column in values)
            {
                string title = (string)column[0];

                int i = (int)column[1];

                //string buttonImg = $"{title}.jpeg";


                //labels
                char[] seperator = new char[] { ' ', ',' };
                string labels_ = (string)column[3];
                List<string> labels = labels_.Split(seperator, StringSplitOptions.RemoveEmptyEntries).ToList();

                //_nums
                int ingredients_num = (int)column[4];
                int howto_num = (int)column[5];
                int nutritionTips_num = (int)column[6];
                int cookingTips_num = (int)column[7];

                //ingredients
                List<string> ingredients_raw = (List<string>)column.Skip(8).Take(ingredients_num);
                List<ingredientBlock> ingredients = new List<ingredientBlock>();

                foreach (var value_raw in ingredients_raw)
                {
                    ingredientBlock ingredient = new ingredientBlock { text = value_raw };
                    ingredients.Add(ingredient);
                }

                //howto
                List<string> howto_raw = (List<string>)column.Skip(8 + 1 + ingredients_num).Take(howto_num);
                List<ingredientBlock> howto = new List<ingredientBlock>();
                char howtoSeperator = '#';

                foreach (string value_raw in howto_raw)
                {
                    var value = value_raw.Split(howtoSeperator).ToList();

                    ingredientBlock ingredient = new ingredientBlock { step = value[0], text = value[1] };

                    howto.Add(ingredient);
                }


                recipeList.list.Add(new recipeModel { title = title, howto = howto, i = i, ingredients = ingredients, labels = labels });

                //nutritionTips

                //cookingTips_ 

                // create new recipemodel
                // title = 0, index = 1, buttonImg = 2, labels = 3
                // ingredients_num = 4, methods_num = 5, nutritionTips_num = 6
                // cookingTips_num = 7, ingredients = 8:8+ingredients_num-1, 
            }
        }



    }
}
}

感谢任何回应:D

【问题讨论】:

  • 您是否将client_secret.json 存储在您遇到的错误中显示的目录中?
  • 问题是我做不到。显示的目录基本上是构建应用程序中的内容。我正在尝试让程序将 client_secret.json 放入应用程序

标签: c# ios xamarin.forms google-sheets-api


【解决方案1】:

我的程序说找不到 client_secret.json 文件,我也找不到解决问题的方法。

首先,将你的 client_secret.json 文件放入共享代码中,确保将 Build Action 设置为 EmbeddedResource

然后使用下面的代码来解析我们本地的json文件。

var assembly = typeof(listviewsample.Page24).GetTypeInfo().Assembly;
        Stream stream = assembly.GetManifestResourceStream("FormsSample.user.json");
        using (var reader = new System.IO.StreamReader(stream))
        {
            var jsonString = reader.ReadToEnd();
           //........
        }

更新:

我在简单的示例中测试了代码,你可以看看下面的截图。

 public partial class Page2 : ContentPage
{
    public Page2()
    {
        InitializeComponent();
        var assembly = typeof(Page2).GetTypeInfo().Assembly;
        Stream stream = assembly.GetManifestResourceStream("mediasample.user.json");
        using (var reader = new System.IO.StreamReader(stream))
        {
            var jsonString = reader.ReadToEnd();

            //Converting JSON Array Objects into generic list                             

        }
    }
}

截图:

【讨论】:

  • 您好,谢谢您的回复。但是什么是'listviewsample.Page24'?
  • FormsSample.User.json 是什么?抱歉,如果这个问题反映了我在 xamarin 表单中的退化。
  • @AdrianWong 比如我的项目名称是FormsSample,我在FormsSample项目中新建文件夹名称listviewsample,创建ContentPage名称Page24,最后在FormsSample根路径下添加User.json文件,设置Build EmbeddedResource 的操作
  • @AdrianWong 请看我的更新代码和截图。
  • 我看到了你更新的代码。但是,由于我在一个单独的类“GoogleSheets.cs”中执行此操作,并且它不是页面或任何东西(只是一个类),如果我用 GoogleSheets 替换更新代码中的“Page2”可以吗?跨度>
【解决方案2】:

感谢 Cherry BU,我克服了这个障碍。这是我的代码:

public GoogleSheets()
    {
        GoogleCredential credential;

        var assembly = typeof(GoogleSheets).GetTypeInfo().Assembly;
        Stream stream = assembly.GetManifestResourceStream("CUapp.client_secret.json");
        using (var reader = new StreamReader(stream))
        {
           
            credential = GoogleCredential.FromStream(stream)
            .CreateScoped(Scopes);
        }

        // Create Google Sheets API service.
        service = new SheetsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential, ApplicationName = ApplicationName                
        });
        ReadEntries();
    }

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 2021-01-04
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多