首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为我的OAuth web应用程序获得Google asp.net 2刷新令牌?

如何为我的OAuth web应用程序获得Google asp.net 2刷新令牌?
EN

Stack Overflow用户
提问于 2015-04-16 14:12:57
回答 1查看 2.5K关注 0票数 2

我试图在我的Asp.net应用程序(C#)中使用Asp.net 2。问题是,我需要使用共享的谷歌帐户。为此,我的计划是使用令牌、过期日期和刷新令牌为身份验证添加种子,然后当需要身份验证时,我检查过期日期并使用刷新令牌。

我一直用于身份验证的示例如下所示:

代码语言:javascript
运行
复制
    UserCredential credential;
    using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
    {
        credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            // This OAuth 2.0 access scope allows an application to upload files to the
            // authenticated user's YouTube channel, but doesn't allow other types of access.
            new[] { YouTubeService.Scope.YoutubeUpload },
            "user",
            CancellationToken.None
        );
    }

并且似乎不包含具有刷新令牌的对象。

如何获得刷新令牌和过期日期?

EN

Stack Overflow用户

回答已采纳

发布于 2015-04-16 17:24:36

解决方案是执行post操作并手动解析结果,而不是使用任何Google类。

代码语言:javascript
运行
复制
        string gurl = "code=" + code + "&client_id=" + client_id +
                "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=" + grant_type;

        string url = "https://www.googleapis.com/oauth2/v3/token";

        // creates the post data for the POST request
        string postData = (gurl);

        // create the POST request
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Host ="www.googleapis.com";

        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = postData.Length;

        // POST the data
        using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
        {
            requestWriter2.Write(postData);
        }

        //This actually does the request and gets the response back
        HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();

        string googleAuth;

        using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
        {
            //dumps the HTML from the response into a string variable
            googleAuth = responseReader.ReadToEnd();
        }

从这里开始,我主要需要解析googleAuth字符串来获取令牌、刷新令牌和过期时间。我一直期待Google类内部有一个解决方案,这肯定是一个非常常见的请求,但显然我会创建自己的类。

票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29677364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档