从API获取谷歌电子邮件

问题描述 投票:0回答:3

我已授权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();
}

提前致谢。

.net google-api google-drive-sdk google-api-client
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.