###前提・実現したいこと
Visual Studio 2015でアプリ作成中です。
C#からGoogle Sheets APIを呼び出したいのですが、実行するとエラーメッセージが表示されます。
下記コード31行目のcredentialの中身がnullになっているのですが、原因がよく分かりません。
もし分かりましたら、教えていただけると助かります。
###発生している問題・エラーメッセージ
例外がスローされました: 'System.AggregateException' (mscorlib.dll の中) 追加情報:1 つ以上のエラーが発生しました。
###該当のソースコード
C#
1using System; 2using System.IO; 3using System.Collections.Generic; 4using System.Threading; 5using Google.Apis.Auth.OAuth2; 6using Google.Apis.Sheets.v4; 7using Google.Apis.Sheets.v4.Data; 8using Google.Apis.Services; 9using Google.Apis.Util.Store; 10 11namespace SheetsApiv4Sample 12{ 13 class Program 14 { 15 static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly }; 16 static string ApplicationName = "Google Sheets API .NET Quickstart"; 17 static string AppClientId = "(クライアント ID)"; 18 static string AppClientSecret = "(クライアント シークレット)"; 19 static string SpreadSheetId = "(操作対象となるシートのID)"; 20 21 public static void Main(string[] args) 22 { 23 UserCredential credential; 24 string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); 25 credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json"); 26 credential = GoogleWebAuthorizationBroker.AuthorizeAsync( 27 new ClientSecrets 28 { 29 ClientId = AppClientId, 30 ClientSecret = AppClientSecret 31 }, 32 Scopes, 33 "user", 34 CancellationToken.None, 35 new FileDataStore(credPath, true) 36 ).Result; 37 //Console.WriteLine("Credential file saved to: " + credPath); 38 39 var service = new SheetsService(new BaseClientService.Initializer() 40 { 41 HttpClientInitializer = credential, 42 ApplicationName = ApplicationName, 43 }); 44 45 //Sheet1のセルA1の値取得 46 SpreadsheetsResource.ValuesResource.GetRequest req1 = service.Spreadsheets.Values.Get(SpreadSheetId, "Sheet1!A1"); 47 IList<IList<Object>> values = req1.Execute().Values; 48 Console.WriteLine(values[0][0]); 49 Console.ReadKey(true); 50 } 51 } 52}
###補足情報
下記サイトを参考にしました。
https://www.ka-net.org/blog/?p=7007
※ソースコード内のクライアントID、クライアントシークレット、操作対象となるシートのIDは正しいものに書き換えた上で実行しています。
回答1件
あなたの回答
tips
プレビュー