【问题标题】:Get google Email from API从 API 获取谷歌电子邮件
【发布时间】: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


【解决方案1】:

将此网址添加到范围:

https://www.googleapis.com/auth/userinfo.profile

它会加载姓名、公开资料网址、电子邮件、性别、照片等。

在您获得授权后,从此 Json 响应中获取信息:

https://www.googleapis.com/oauth2/v1/userinfo?alt=json

 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"
        "https://www.googleapis.com/auth/userinfo.email"
    });

编辑

将范围添加到您的 AuthorizationState 构造函数

 List<string> scopes = new List<string>();                
 scopes.Add("https://www.googleapis.com/auth/userinfo.email");

【讨论】:

  • 谢谢。没有办法使用 API 获得这个?
  • 我没听懂。您如何在这里收到电子邮件?
【解决方案2】:
  1. 添加范围: https://www.googleapis.com/auth/userinfo.profile
  2. 使用此代码:

    private string FetchUsersEmail()
    {
        var emailRequest = @"https://www.googleapis.com/userinfo/email?alt=json&access_token=" + GoogleAccessToken;
        string jsonString;
        var request = WebRequest.Create(emailRequest);
    
        using (var response = (HttpWebResponse)request.GetResponse())
        using (var dataStream = response.GetResponseStream())
        using (var reader = new StreamReader(dataStream))
        {
            jsonString = reader.ReadToEnd();
        }
    
        var json = JToken.Parse(jsonString);
        var currentGoogleEmail = json["data"]["email"].Value<string>();
    
        return currentGoogleEmail;
    }
    

    我使用 Newtonsoft 解析 json,但您可以使用任何其他库。

【讨论】:

    【解决方案3】:

    使用About.get()

    DriveService driveService = ...
    
    var req = driveService.About.Get();
    req.Fields = "user(displayName,photoLink, me, emailAddress), storageQuota(limit,usage), maxImportSizes, maxUploadSize";
    About ab = await req.ExecuteAsync();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-07
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 2013-11-25
      • 2020-08-29
      • 2013-04-04
      相关资源
      最近更新 更多