【问题标题】:How to get oauth access token in console without authentication prompt如何在没有身份验证提示的情况下在控制台中获取 oauth 访问令牌
【发布时间】:2016-12-15 02:19:04
【问题描述】:

我想像 oauth 一样进行身份验证 Login using Google OAuth 2.0 with C# 但我不想验证提示弹出 我想直接获取令牌而不弹出..

public  ActionResult CodeLele()
    {
        if (Session.Contents.Count > 0)
        {
            if (Session["loginWith"] != null)
            {
                if (Session["loginWith"].ToString() == "google")
                {
                    try
                    {
                        var url = Request.Url.Query;
                        if (url != "")
                        {
                            string queryString = url.ToString();
                            char[] delimiterChars = { '=' };
                            string[] words = queryString.Split(delimiterChars);
                            string code = words[1];

                            if (code != null)
                            {
                                //get the access token 
                                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
                                webRequest.Method = "POST";
                                Parameters = "code=" + code + "&client_id=" + googleplus_client_id + "&client_secret=" + googleplus_client_sceret + "&redirect_uri=" + googleplus_redirect_url + "&grant_type=authorization_code";
                                byte[] byteArray = Encoding.UTF8.GetBytes(Parameters);
                                webRequest.ContentType = "application/x-www-form-urlencoded";
                                webRequest.ContentLength = byteArray.Length;
                                Stream postStream = webRequest.GetRequestStream();
                                // Add the post data to the web request
                                postStream.Write(byteArray, 0, byteArray.Length);
                                postStream.Close();

                                WebResponse response = webRequest.GetResponse();
                                postStream = response.GetResponseStream();
                                StreamReader reader = new StreamReader(postStream);
                                string responseFromServer = reader.ReadToEnd();

                                GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseFromServer);

                                if (serStatus != null)
                                {
                                    string accessToken = string.Empty;
                                    accessToken = serStatus.access_token;

                                    if (!string.IsNullOrEmpty(accessToken))
                                    {
                                        // This is where you want to add the code if login is successful.
                                        // getgoogleplususerdataSer(accessToken);
                                    }
                                    else
                                    { }
                                }
                                else
                                { }
                            }
                            else
                            { }
                        }
                    }
                    catch (WebException ex)
                    {
                        try
                        {
                            var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

                            dynamic obj = JsonConvert.DeserializeObject(resp);
                            //var messageFromServer = obj.error.message;
                            //return messageFromServer;
                            return obj.error_description;
                        }
                        catch (Exception exc)
                        {
                            throw exc;
                        }
                    }
                }
            }
        }
        return Content("done");
    }
    public ActionResult JeClick()


    {
        var Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + googleplus_redirect_url + "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" + googleplus_client_id;
        Session["loginWith"] = "google";
        return Redirect(Googleurl);
    }

【问题讨论】:

    标签: asp.net-mvc c#-4.0 google-api google-oauth


    【解决方案1】:

    凭据窗口(弹出窗口)是您询问用户是否可以访问他们的数据的方式。如果不先询问用户是否可以访问他们的数据,就无法访​​问用户数据。这就是 Oauth2 的工作原理。

    如果您要访问自己的数据,则可以使用称为服务帐户的东西。 Service accounts 是预授权的。您可以使用服务帐户并授予它访问您的谷歌日历的权限,您可以授予它访问 Google 驱动器中的文件夹的权限。然后,您可以使用服务帐户进行身份验证。服务帐户就像虚拟用户。

    我的关于服务帐号的文章:Google Developer service account

    【讨论】:

    • 但这是我自己的帐户,我想通过它访问网上商店 Api,我只需要访问令牌,为此它需要一个代码,该代码将与重定向 URI 一起发送
    • 然后根据您将要使用的 API,您应该查看正在使用的服务帐户。
    • 我尝试了很多次但我无法限制打开允许访问弹出窗口
    • 服务帐户没有允许访问弹出窗口,您应该尝试阅读我在答案中为您提供的链接。服务帐户与 Oauth2 完全不同。
    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2018-04-14
    • 2019-05-22
    • 2021-11-03
    • 2018-02-23
    • 2019-05-10
    • 2017-05-31
    • 2022-03-03
    相关资源
    最近更新 更多