我已授权DriveService
对象。有没有办法获得授权用户的电子邮件?我可以获取显示名称,但不能获取电子邮件地址。
OAuth2Authenticator auth = new OAuth2Authenticator<NativeApplicationClient>(provider, client => GetAuthorization(client));
DriveService service = new DriveService(
new BaseClientService.Initializer()
{
Authenticator = auth
}
);
Console.WriteLine(service_.About.Get().Fetch().User.DisplayName);
GetAuthorization
函数打开浏览器并允许用户授权。
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new[]
{
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.metadata.readonly",
"https://www.googleapis.com/auth/drive.readonly"
});
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
using (LoginWindow login = new LoginWindow(arg.RequestUserAuthorization(state).ToString()))
{
if (login.ShowDialog() == DialogResult.OK)
{
return arg.ProcessUserAuthorization(login.AuthorizationToken, state);
}
}
throw new UnauthorizedAccessException();
}
提前致谢。