【问题标题】:how to add profile scope and email scopes in Google OAuth in C#(Windows form)如何在 C#(Windows 窗体)的 Google OAuth 中添加配置文件范围和电子邮件范围
【发布时间】:2020-12-09 11:00:14
【问题描述】:

如何在 Google OAuth 中添加配置文件范围和电子邮件范围,我正在使用下面给出的代码。

        string tokenRequestURI = "https://www.googleapis.com/oauth2/v4/token";
        string tokenRequestBody = string.Format("code={0}&redirect_uri={1}&client_id={2}&code_verifier={3}&client_secret={4}&scope=&grant_type=authorization_code",
            code,
            System.Uri.EscapeDataString(redirectURI),
            clientID,
            code_verifier,
            clientSecret
            );

        // sends the request
        HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(tokenRequestURI);
        tokenRequest.Method = "POST";
        tokenRequest.ContentType = "application/x-www-form-urlencoded";
        tokenRequest.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        byte[] _byteVersion = Encoding.ASCII.GetBytes(tokenRequestBody);
        tokenRequest.ContentLength = _byteVersion.Length;
        Stream stream = tokenRequest.GetRequestStream();
        await stream.WriteAsync(_byteVersion, 0, _byteVersion.Length);
        stream.Close();

【问题讨论】:

    标签: c# oauth google-api scope


    【解决方案1】:

    您没有在您的 tokenRequestBody 中发送任何范围您是否尝试将它们添加到那里?

    string tokenRequestBody = string.Format("code={0}&redirect_uri={1}&client_id={2}&code_verifier={3}&client_secret={4}&scope={5}&grant_type=authorization_code",
            code,
            System.Uri.EscapeDataString(redirectURI),
            clientID,
            code_verifier,
            clientSecret,
            "email profile"
            );
    

    您应该真正考虑使用 Google .net 客户端库,而不是全部手动完成。

    【讨论】:

    • 我已添加此代码。仍然没有得到电子邮件地址。我在谷歌云平台中添加了范围。你能检查一下stackoverflow.com/questions/65217242/…(在谷歌云平台中添加了范围)
    猜你喜欢
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多