【发布时间】:2013-05-24 07:55:52
【问题描述】:
我已授权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();
}
提前致谢。
【问题讨论】:
-
您能告诉我们您当前的代码吗?
-
您正在使用 Google Drive 来获取用户详细信息?你走错方向了。用户对象不包含电子邮件。参考:developers.google.com/drive/v2/reference/about/get
-
我没有使用谷歌驱动器来获取用户详细信息。我正在使用谷歌驱动器访问用户存储,但我需要知道授权用户的电子邮件。在我看来这很合理。如果 API 允许授权,它也应该允许获取授权的用户详细信息。
-
Drive API 不提供此类信息。您必须使用其他 API ,如下面的答案所述。
标签: .net google-api google-drive-api google-api-client