繁体   English   中英

C#Google OAuth2获取当前登录用户的电子邮件

[英]C# Google OAuth2 get email of the user currently login

我最近才尝试使用Google Apis,这是一个全新的尝试。 我花了最后2个小时来搜索如何获取允许访问API的电子邮件以及如何自动刷新令牌。

我的代码与教程基本相同

class GoogleAPI
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
        static string[] Scopes = { SheetsService.Scope.Spreadsheets };
        static string ApplicationName = "Google Sheets API .NET Quickstart";

        public GoogleAPI()
        {
            UserCredential credential;

            using (var stream =
                new FileStream("Secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/Secret_Credentials.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;  
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Google Sheets API service.
            var service = new SheetsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

            // Define request parameters.
            String spreadsheetId = "1Jd9NgmnfvJzPtjXw_jNzSUAlOO532mF6ebYyIIle_H8";
            String range = "Sheet1!A:L";
            SpreadsheetsResource.ValuesResource.GetRequest request =
                    service.Spreadsheets.Values.Get(spreadsheetId, range);

            ValueRange response = request.Execute();

            var a = response;
            System.Windows.Forms.MessageBox.Show("success");

        }
    }

搜索后,我假设我的令牌过期后会自动刷新,所以我有1个问题。

问题:我如何获取保存在凭据中的用户电子邮件。

我真的很陌生,请通过详细解释帮助我。


感谢@DaImTo

我终于知道如何添加它。 他的教程-> http://www.daimto.com/find-users-email-with-google-api/

使它起作用的代码。

首先,我们需要添加“电子邮件”作为范围,并记住删除您的旧凭据。

static string[] Scopes = { SheetsService.Scope.Spreadsheets, "email" };

其次,为google plus创建服务。

// Create Google Plus API service.
var plusService = new PlusService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});

最后,获取电子邮件

var me = plusService.People.Get("me").Execute();
var useremail = me.Emails.FirstOrDefault().Value;

您为什么要电子邮件呢?

访问令牌允许访问API。 刷新令牌将在FileDataStore进行的过期(1小时)到期时自动更新访问令牌。

如果我正确读取credPath路径,则文件数据存储区已将您的凭据保存在%appData%中。

我知道要获取当前经过身份验证的用户的电子邮件的唯一方法是通过Google+ API People.get发送给我。 您将需要在请求中添加另一个范围。

如果使用userId值“ me”,则此方法需要使用已授予OAuth作用域https://www.googleapis.com/auth/plus.loginhttps://www.googleapis.com/auth的令牌的身份验证/plus.me 进一步了解OAuth。

码:

Nuget包:

PM> Install-Package Google.Apis.Plus.v1

var me = plusService.People.Get("me").Execute();
var useremail = me.Emails.FirstOrDefault().Value;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM