【发布时间】:2021-01-07 22:59:53
【问题描述】:
我正在编写一个 Discord Bot (Discord.net),我发现自己需要使用他们的 APIs 访问谷歌表格上的一些数据。因为我认为最好将这两个实际分离到两个不同的类文件中,我尝试将 Google API 的 Main 方法调用到我的程序中(在将其重命名为“Sheets") 在我的 Program.cs 中像这样:
using Discord;
using Discord.WebSocket;
using System;
using System.IO;
using System.Threading.Tasks;
namespace WoM_Balance_Bot
{
public class Program
{
public static void Main(string[] args)
{
GoogleAPI GSheet = new GoogleAPI();
GSheet.Sheets();
new Program().MainAsync().GetAwaiter().GetResult();
}
private DiscordSocketClient _client;
public async Task MainAsync()
{
_client = new DiscordSocketClient();
_client.MessageReceived += CommandHandler;
_client.Log += Log;
var token = File.ReadAllText("bot-token.txt");
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
// Block this task until the program is closed.
await Task.Delay(-1);
}.......ecc
我尝试编写参数传递到这里in these parentheses,例如“string”和“args”但要么我的语法错误,要么我对确切传递的内容有一个非常错误的想法。
这是 GoogleAPI.cs 的实际内容,这是我创建的另一个具有 Google Sheet API 的类文件:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace WoM_Balance_Bot
{
public class GoogleAPI
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
private static readonly string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
private static readonly string ApplicationName = "wombankrolls";
public static void Sheets(string[] args)
{
UserCredential credential;
Console.WriteLine("if you read this then it's good");
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define request parameters.
String spreadsheetId = "16W56LWqt6wDaYAU5xNdTWCdaY_gkuQyl4CE1lPpUui4";
String range = "Class Data!G163:I";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);
// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
/*
if (values != null && values.Count > 0)
{
Console.WriteLine("Name, Major");
foreach (var row in values)
{
// Print columns A and E, which correspond to indices 0 and 4.
Console.WriteLine("{0}, {1}", row[0], row[4]);
}
}
else
{
Console.WriteLine("No data found.");
}
Console.Read();
*/
}
}
}
我已经从谷歌给出的quickstart 修改了它,我认为这是有道理的,但我最终还是得到了同样的错误:
没有给出与“GoogleAPI.Sheets(string[])”的所需形式参数“args”相对应的参数
【问题讨论】:
-
该消息有什么不清楚的地方?您需要传递
args参数。虽然考虑到您在Sheets函数中不使用 if ,但您不妨删除那里的参数。 -
为什么要在 GoogleAPI.Sheets 类方法中添加
string[] args参数?你从来不用它。 -
我实际上读到,在处理 API 时,您应该始终保持参数打开,我的第一个想法是也将其删除
-
作为一般经验法则,如果您不使用参数,请将其删除。如果您的方法需要一个参数并且您没有提供一个参数,C# 会通过引发编译器错误来帮助强制执行此范例,这正是这里发生的情况。
标签: c# google-sheets-api discord.net