#android #android-studio #oauth-2.0 #google-api #google-calendar-api
Вопрос:
Поэтому мне нужно обновить статус приглашения участника в моем мероприятии Google Meet, созданном с помощью API календаря. Я должен проверить, принял ли участник приглашение, и перенаправить его соответствующим образом. Но если мне нужен адрес электронной почты участника, который входит в систему с помощью OAuth2.0, я не могу его найти.
Я не использую GoogleCredentials для хранения токена, так как не знаю, как найти токен доступа после проверки. Я сослался на несколько вопросов, похожих на мои, но, похоже, не могу найти правильного решения.
Мой код выглядит следующим образом :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport();
credential=getCredentials(getApplicationContext(), HTTP_TRANSPORT);
service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY,credential )
.setApplicationName(getResources().getString(R.string.app_name))
.build();
// List the next 10 events from the primary calendar.
DateTime now = new DateTime(System.currentTimeMillis());
} catch (IOException e) {
e.printStackTrace();
}
}
private static Credential getCredentials(Context context, final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
GoogleAuthorizationCodeFlow flow;
InputStream in = OnlineSubjectVideoList.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
File tokenFolder = new File(context.getExternalFilesDir("").getAbsolutePath()
TOKENS_DIRECTORY_PATH);
Log.e(DashboardPrincipal.class.getSimpleName(), "getCredentials: " context.getExternalFilesDir("").getAbsolutePath()
TOKENS_DIRECTORY_PATH );
if (!tokenFolder.exists()) {
tokenFolder.mkdirs();
}
flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(tokenFolder))
.setAccessType("offline")
.build();
Log.e(DashboardPrincipal.class.getSimpleName(), "getCredentials: " clientSecrets.getDetails().getClientSecret() );;
// LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
AuthorizationCodeInstalledApp ab = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) {
protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
String url = (authorizationUrl.build());
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(browserIntent);
}
};
return ab.authorize("user");
}
Следующий код предназначен для обновления событий, где мне нужен адрес электронной почты
final Event event = new Event()
.setAttendees(Arrays.asList(new EventAttendee().setEmail(email)
.setResponseStatus("accepted")));
try {
service.events()
.patch(Calendar_id, Event_Id, event)
.setSendNotifications(true)
.execute();
} catch (final Exception ex) {
}