【发布时间】: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