简体   繁体   English

Google Drive REST API Windows表单登录

[英]Google Drive REST API Windows Forms login

I'm having a problem retrieving the access token from the redirect uri. 我在从重定向uri检索访问令牌时遇到问题。 When I try my coded procedure on my mozilla I get redirected to my localhost with the access token, but how do I instantiate this with my webBrowser in C#? 当我在mozilla上尝试编码过程时,我使用访问令牌重定向到了本地主机,但是如何在C#中使用webBrowser实例化它呢? My current code looks like this: 我当前的代码如下所示:

public partial class Form1 : Form
{
    public string client_id = "CLIENT_ID";
    public string client_secret= "CLIENT_SECRET";
    public string redirect_uri = "http://localhost:5050";
    public Form1()
    {
        InitializeComponent();

        webBrowser.AllowNavigation = true;
        webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
        webBrowser.Navigate(String.Format("https://accounts.google.com/o/oauth2/auth?response_type=code&scope=email+profile&redirect_uri={0}&client_id={1}&hl=de&from_login=1&as=d832bdaf61552d&pli=1&authuser=0"
            ,redirect_uri, client_id));

    }

    void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = sender as WebBrowser;
        //MessageBox.Show(wb.Url.ToString());
        if (wb.Url.ToString().StartsWith("http://localhost"))
        {

            String[] s = wb.Url.ToString().Split('=');
            string authCode = s[s.Length - 1];

            MessageBox.Show("auth Code: " + authCode);

            //WebClient wc = new WebClient();

            //var data = new NameValueCollection();
            //data["code"] = authCode;
            //data["client_id"] = client_id;
            //data["client_secret"] = client_secret;
            //data["redirect_uri"] = "http://localhost:5050";
            //data["grant_type"] = "authorization_code";

            //var response = wc.UploadValues("accounts.google.com/o/oauth2/token", "POST", data);

            //foreach (byte a in response)
            //{
            //    MessageBox.Show(a.ToString());
            //}
        }
    }

If you know how to do this with the GoogleDrive API please share it with me. 如果您知道如何使用GoogleDrive API进行操作,请与我分享。

Please Use UIWebBrowser instead of WebBrowser and your code should like this. 请使用UIWebBrowser而不是WebBrowser,您的代码应与此类似。

public Form1()
{
    InitializeComponent();

    UIWebBrowser.AllowNavigation = true;
    UIWebBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
    UIWebBrowser.Navigate(String.Format("https://accounts.google.com/o/oauth2/auth?response_type=code&scope=email+profile&redirect_uri={0}&client_id={1}&hl=de&from_login=1&as=d832bdaf61552d&pli=1&authuser=0"
        ,redirect_uri, client_id));

}

void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
   if (e.Url.AbsoluteUri.StartsWith("http://localhost"))
     {
            // Redirect so we end up with a blank web browser control
            UIWebBrowser.Navigate("about:blank");

           code = e.Url.Query;


    }
}

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

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