質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

2833閲覧

Google Calendar API Java Quickstart

wangzj

総合スコア53

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2019/10/03 07:35

編集2019/10/04 05:43

前提・実現したいこと

Google Calendar API Java Quickstart
Javaでカレンダーのイベントのサマリーを変更したいです。
イベント情報読取は大丈夫です。イベントの項目を変更するとエラーとなります。

発生している問題・エラーメッセージ

10 03, 2019 6:23:59 午後 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly 警告: unable to change permissions for everybody: C:\20190301\BCL\develop\test\test\tokens 10 03, 2019 6:23:59 午後 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly 警告: unable to change permissions for owner: C:\20190301\BCL\develop\test\test\tokens Upcoming events Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Insufficient Permission: Request had insufficient authentication scopes.", "reason" : "insufficientPermissions" } ], "message" : "Insufficient Permission: Request had insufficient authentication scopes." } at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:451) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1089) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:549) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:482) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:599) at test.test.App.main(App.java:119)

ソース

java

1package test.test; 2 3import com.google.api.client.auth.oauth2.Credential; 4import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; 5import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; 6import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; 7import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; 8import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 9import com.google.api.client.http.javanet.NetHttpTransport; 10import com.google.api.client.json.JsonFactory; 11import com.google.api.client.json.jackson2.JacksonFactory; 12import com.google.api.client.util.DateTime; 13import com.google.api.client.util.store.FileDataStoreFactory; 14import com.google.api.services.calendar.Calendar; 15 16import com.google.api.services.calendar.CalendarScopes; 17import com.google.api.services.calendar.model.Event; 18import com.google.api.services.calendar.model.Events; 19 20 21import java.io.FileInputStream; 22import java.io.FileNotFoundException; 23import java.io.IOException; 24import java.io.InputStream; 25import java.io.InputStreamReader; 26import java.security.GeneralSecurityException; 27import java.sql.Date; 28import java.text.ParseException; 29import java.text.SimpleDateFormat; 30import java.time.format.DateTimeFormatter; 31import java.util.Collections; 32import java.util.List; 33 34import org.apache.commons.lang3.time.DateFormatUtils; 35import org.joda.time.format.DateTimeFormat; 36import org.joda.time.format.ISODateTimeFormat; 37 38/** 39 * Hello world! 40 * 41 */ 42public class App 43{ 44 private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart"; 45 private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 46 private static final String TOKENS_DIRECTORY_PATH = "tokens"; 47 48 /** 49 * Global instance of the scopes required by this quickstart. 50 * If modifying these scopes, delete your previously saved tokens/ folder. 51 */ 52 private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR); 53 private static final String CREDENTIALS_FILE_PATH = "C:\test\client_secret.json"; 54 55 /** 56 * Creates an authorized Credential object. 57 * @param HTTP_TRANSPORT The network HTTP Transport. 58 * @return An authorized Credential object. 59 * @throws IOException If the credentials.json file cannot be found. 60 */ 61 private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException { 62 // Load client secrets. 63 //InputStream in = CalendarQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); 64 InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH); 65 if (in == null) { 66 throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH); 67 } 68 GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 69 70 // Build flow and trigger user authorization request. 71 72 GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( 73 HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 74 .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) 75 .setAccessType("offline") 76 .build(); 77 LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); 78 return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); 79 } 80 81 public static void main( String[] args ) throws IOException, GeneralSecurityException, ParseException 82 { 83 System.out.println( "Hello World!" ); 84 $client = new Google_Client(); 85 $client->useApplicationDefaultCredentials(); 86 $client->addScope(Google_Service_Pubsub::PUBSUB); 87 // Build a new authorized API client service. 88 final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 89 Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) 90 .setApplicationName(APPLICATION_NAME) 91 .build(); 92String calendarId = "******************ghs4@group.calendar.google.com"; 93 // List the next 10 events from the primary calendar. 94 DateTime now = new DateTime(System.currentTimeMillis()); 95 96 Events events = service.events().list(calendarId) 97 .setMaxResults(100) 98 .setTimeMin(now) 99 .setOrderBy("startTime") 100 .setSingleEvents(true) 101 .execute(); 102 103 List<Event> items = events.getItems(); 104 105 if (items.isEmpty()) { 106 System.out.println("No upcoming events found."); 107 } else { 108 System.out.println("Upcoming events"); 109 for (Event event : items) { 110 DateTime start = event.getStart().getDateTime(); 111 DateTime end = event.getEnd().getDateTime(); 112 String email = event.getCreator().getEmail(); 113 String eventId = event.getId(); 114 115 event.set("summary", event.getSummary() + " 数字:123456"); 116 if (start == null) { 117 start = event.getStart().getDate(); 118 } 119 service.events().update(calendarId, eventId, event).execute(); 120 System.out.printf("%s (%s)\n", event.getSummary(), start.toStringRfc3339()); 121 } 122 } 123 } 124} 125

試したこと

SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);
CALENDARに変えいました。>>>ダメです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

この行↓を削除して、実行できました。
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))

補足:
↑の方法で実行できましたが、トークンが保存されてません。
TOKENS_DIRECTORY_PATHに存在してるトークンファイルを削除すれば、↑のコードを削除せず、実行もできました。

投稿2019/10/04 05:39

編集2019/10/04 08:29
wangzj

総合スコア53

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問