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.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
//https://docs.google.com/spreadsheets/d/1ENivPDO7g7cbjzu_ljOfsAzTVJofx1d7HvDCoBp6dh4/edit#gid=0
static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "Update Google Sheet Data with Google Sheets API v4";
static String spreadsheetId = "1ENivPDO7g7cbjzu_ljOfsAzTVJofx1d7HvDCoBp6dh4";
static string sheetName = "sheet1";
static void Main(string[] args)
{
var service = OpenSheet();
UpdateRow(service);
}
static SheetsService OpenSheet()
{
UserCredential credential;
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = Path.Combine
(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
".credentials/sheets.googleapis.com-dotnet-quickstart.json");
//存儲憑證到credPath
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
//建立一個API服務,設定請求參數
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
return service;
}
static void UpdateRow(SheetsService service)
{
ValueRange rVR;
String sRange;
int rowNumber = 1;
//設定讀取A欄最後一行位置
sRange = String.Format("{0}!A:A", sheetName);
SpreadsheetsResource.ValuesResource.GetRequest getRequest
= service.Spreadsheets.Values.Get(spreadsheetId, sRange);
rVR = getRequest.Execute(); //到Google sheet讀取內容
IList<IList<Object>> values = rVR.Values; //最後一行位置
//寫入新資料
if (values != null && values.Count > 0) rowNumber = values.Count + 1; //添加一行
sRange = String.Format("{0}!A{1}:B{1}", sheetName, rowNumber); //指定寫入位置
//設定寫入
ValueRange valueRange = new ValueRange();
valueRange.Range = sRange;
valueRange.MajorDimension = "ROWS";//ROWS或COLUMNS
//取得當前時間
DateTime dt = new DateTime();
dt = DateTime.Now;
List<object> oblist = new List<object>() { String.Format("{0}", rowNumber), dt.ToString("HH:mm:ss") };
//寫入時間
valueRange.Values = new List<IList<object>> { oblist };
Console.WriteLine("{0}, {1}", oblist[0], oblist[1]);
//執行寫入動作
SpreadsheetsResource.ValuesResource.UpdateRequest updateRequest
= service.Spreadsheets.Values.Update(valueRange, spreadsheetId, sRange);
updateRequest.ValueInputOption
= SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
UpdateValuesResponse uUVR = updateRequest.Execute();
}
}
}
到credential = GoogleWebAuthorizationBroker.AuthorizeAsync 的時候要登入帳號
他跟我說沒權限
我有在OAuth2.0 下載Json 放入 專案取名為 client_secret.json
也在 heets.googleapis.com-dotnet-quickstart.json 放入一樣的檔案
系統跳出
錯誤代碼 403: access_denied
The developer hasn’t given you access to this app. It’s currently being tested and it hasn’t been verified by Google. If you think you should have access, contact the developer