有没有办法检查UserCredential是否已使用用户名登录?

时间:2015-08-24 01:56:42

标签: c# .net winforms google-api gmail-api

我正在使用google api v3.0

UserCredential uc = null;
UserCredential credential = null;
        private void UserCredentials()
        {
            using (FileStream stream = new FileStream(@"C:\jason file\client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None,
                    new FileDataStore("YouTube.Auth.Store")).Result;
            }
            uc = credential;
        }

然后

private void UserYoutubeService()
        {
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });
        }

我在我的表单构造函数中调用了这两个方法:

public Youtube_Uploader(string filetoupload)
        {
            InitializeComponent();

            FileNameToUpload = @"C:\test.mp4";
            service = AuthenticateOauth(apiKey);
            var videoCatagories = service.VideoCategories.List("snippet");
            videoCatagories.RegionCode = "IL";
            var result = videoCatagories.Execute();
            UserCredentials();
            UserYoutubeService();
            MakeRequest();
            backgroundWorker1.RunWorkerAsync();
        }

现在我遇到了一些问题。

在UserCredentials方法中,这个GoogleWebAuthorizationBroker将启动Chrome浏览器,但是我需要做一些检查它是否已经拥有凭据的内容,如果不这样做,请不要在我的程序上显示我我的表单,它有凭据,并显示凭据的名称。

如果凭据为空,请使用google youtube api的google youtube api屏幕制作GoogleWebAuthorizationBroker并启动Chrome,要求用户登录然后接受。

接受屏幕我的意思是屏幕说:这个应用程序想要.....取消接受

所以也许像GoogleWebAuthorizationBroker这样的东西不会自动触发chrome,但只有在有证书的情况下才会出现。

如果已经有凭据,那么还会给我当前的gmail帐户名称,然后在表单上显示我的帐户名称。

如果它是例如danny@gmail.com,那么请告诉我类似于:连接到:Danny

但如果我退出我的Gmail帐户,它会显示接受屏幕并登录屏幕登录gmail帐户。

如果用户登录,这就是我注销的方式:

private void button1_Click(object sender, EventArgs e)
        {
            Revoke();
        }

然后:

public void Revoke()
        {
            if (uc!=null)
            {
                uc.RevokeTokenAsync(CancellationToken.None);
            }

        }

0 个答案:

没有答案
相关问题