Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# eyValuePair(“客户id”,客户id), 新的KeyValuePair(“客户端密码”,ClientSecret), 新的KeyValuePair(“重定向uri”,重定向URL), 新的KeyValuePair(“访问类型”、“脱机”) }); var result=client.PostAsync(“https://accounts.google.com/o/oauth2/token,nvc)。结果; var resultContent=result.Content.ReadAsStringAsync(); var tokenResponse=JsonConvert.DeserializeObject(resultContent.ToString()); 返回“temp”; } }_C#_Asp.net_Rest_Oauth 2.0 - Fatal编程技术网

C# eyValuePair(“客户id”,客户id), 新的KeyValuePair(“客户端密码”,ClientSecret), 新的KeyValuePair(“重定向uri”,重定向URL), 新的KeyValuePair(“访问类型”、“脱机”) }); var result=client.PostAsync(“https://accounts.google.com/o/oauth2/token,nvc)。结果; var resultContent=result.Content.ReadAsStringAsync(); var tokenResponse=JsonConvert.DeserializeObject(resultContent.ToString()); 返回“temp”; } }

C# eyValuePair(“客户id”,客户id), 新的KeyValuePair(“客户端密码”,ClientSecret), 新的KeyValuePair(“重定向uri”,重定向URL), 新的KeyValuePair(“访问类型”、“脱机”) }); var result=client.PostAsync(“https://accounts.google.com/o/oauth2/token,nvc)。结果; var resultContent=result.Content.ReadAsStringAsync(); var tokenResponse=JsonConvert.DeserializeObject(resultContent.ToString()); 返回“temp”; } },c#,asp.net,rest,oauth-2.0,C#,Asp.net,Rest,Oauth 2.0,[决心]我就是这么做的(在几个朋友的帮助下) 将标题更改为JSON 已将基本URI移动到PostAsync() 一旦我改变了所有我能看到的错误都在重定向Uri中,然后我改变了它以匹配postman中的错误。现在它起作用了 protected void Page_Load(object sender, EventArgs e) { //you will get this, when any error will occur while authorization other

[决心]我就是这么做的(在几个朋友的帮助下)

  • 将标题更改为JSON

  • 已将基本URI移动到PostAsync()

  • 一旦我改变了所有我能看到的错误都在重定向Uri中,然后我改变了它以匹配postman中的错误。现在它起作用了

    protected void Page_Load(object sender, EventArgs e)
        {
            //you will get this, when any error will occur while authorization otherwise null    
            string Error = Request.QueryString["error"];
            //authorization code after successful authorization    
            string Code = Request.QueryString["code"];
            if (Error != null) { }
            else if (Code != null)
            {
                //Remember, we have set userid in State    
                string UserId = Request.QueryString["state"];
                //Get AccessToken    
                int Id = Convert.ToInt32(UserId);
                string AccessToken = string.Empty;
                string RefreshToken = ExchangeAuthorizationCode(Id, Code, out AccessToken);
                //saving refresh token in database    
                SaveRefreshToken(Id, RefreshToken);
                //Get Email Id of the authorized user    
                string EmailId = FetchEmailId(AccessToken);
                //Saving Email Id    
                SaveEmailId(UserId, EmailId);
                //Redirect the user to Authorize.aspx with user id    
                string Url = "Authorize.aspx?UserId=" + UserId;
                Response.Redirect(Url, true);
            }
        }
    
        private string ExchangeAuthorizationCode(int userId, string code, out string accessToken)
        {
            string baseurl = "https://accounts.google.com/o/oauth2/token";
            accessToken = string.Empty;
            string ClientSecret = ConfigurationManager.AppSettings["ClientSecrete"];
            string ClientId = ConfigurationManager.AppSettings["ClientId"];
            // //get this value by opening your web app in browser.    
            string RedirectUrl = "http://localhost:64716/GoogleCallback.aspx"; //I changed this to match the one in Postman
    
            using (var client = new HttpClient())
            {
                //client.BaseAddress = new Uri("https://accounts.google.com/o/oauth2"); //I replaced the Uri to "var result = client.PostAsync("https://accounts.google.com/o/oauth2/token", nvc).Result;"
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // I made this JSON
                // I removed this "client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");"
    
                var nvc = new FormUrlEncodedContent(new[] {
                new KeyValuePair<string, string>("grant_type", "authorization_code"),
                new KeyValuePair<string, string>("code", code),
                new KeyValuePair<string, string>("client_id", ClientId),
                new KeyValuePair<string, string>("client_secret", ClientSecret),
                new KeyValuePair<string, string>("redirect_uri", RedirectUrl),
                new KeyValuePair<string, string>("access_type", "offline")
                });
    
                var result = client.PostAsync("https://accounts.google.com/o/oauth2/token", nvc).Result;
    
                var resultContent = result.Content.ReadAsStringAsync();
    
                var tokenResponse = JsonConvert.DeserializeObject<Authorization_request_body>(resultContent.ToString());
    
                return "temp";
    
                }
    
            }
    
    受保护的无效页面加载(对象发送方,事件参数e)
    {
    //当授权为空时发生任何错误时,您将得到此消息
    string Error=Request.QueryString[“Error”];
    //成功授权后的授权代码
    字符串代码=Request.QueryString[“Code”];
    如果(错误!=null){}
    else if(代码!=null)
    {
    //记住,我们已经将userid设置为State
    字符串UserId=Request.QueryString[“state”];
    //获取AccessToken
    int Id=Convert.ToInt32(UserId);
    string AccessToken=string.Empty;
    string RefreshToken=ExchangeAuthorizationCode(Id、代码、out AccessToken);
    //在数据库中保存刷新令牌
    SaveRefreshToken(Id,RefreshToken);
    //获取授权用户的电子邮件Id
    字符串EmailId=FetchEmailId(AccessToken);
    //保存电子邮件Id
    SaveEmailId(用户ID、EmailId);
    //将用户重定向到具有用户id的Authorize.aspx
    string Url=“Authorize.aspx?UserId=“+UserId;
    重定向(Url,true);
    }
    }
    私有字符串ExchangeAuthorizationCode(int userId、字符串代码、out string accessToken)
    {
    字符串baseurl=”https://accounts.google.com/o/oauth2/token";
    accessToken=string.Empty;
    字符串ClientSecret=ConfigurationManager.AppSettings[“ClientSecret”];
    字符串ClientId=ConfigurationManager.AppSettings[“ClientId”];
    ////通过在浏览器中打开web应用程序获取此值。
    字符串重定向URL=”http://localhost:64716/GoogleCallback.aspx“;//我把这个改成了《邮递员》中的那个
    使用(var client=new HttpClient())
    {
    //client.BaseAddress=新Uri(“https://accounts.google.com/o/oauth2“”;//我将Uri替换为“var result=client.PostAsync(”https://accounts.google.com/o/oauth2/token,nvc)。结果
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);//我制作了这个json
    //我删除了这个“client.DefaultRequestHeaders.Add”(“内容类型”,“应用程序/x-www-form-urlencoded”)
    var nvc=新FormUrlEncodedContent(新[]{
    新的KeyValuePair(“授权类型”、“授权代码”),
    新的KeyValuePair(“代码”,代码),
    新的KeyValuePair(“客户端id”,客户端id),
    新的KeyValuePair(“客户端密码”,ClientSecret),
    新的KeyValuePair(“重定向uri”,重定向URL),
    新的KeyValuePair(“访问类型”、“脱机”)
    });
    var result=client.PostAsync(“https://accounts.google.com/o/oauth2/token,nvc)。结果;
    var resultContent=result.Content.ReadAsStringAsync();
    var tokenResponse=JsonConvert.DeserializeObject(resultContent.ToString());
    返回“temp”;
    }
    }
    

  • 我没见过oauth代币试图这样发送。您的头文件是否与postman头文件匹配?在这个特定的示例中,否,在postman中,我使用application/x-www-form-urlencoded而不是application/json,但我也尝试过添加它。它并没有解决我的问题。我没有看到oauth令牌试图这样发送。您的头文件是否与postman头文件匹配?在这个特定的示例中,否,在postman中,我使用application/x-www-form-urlencoded而不是application/json,但我也尝试过添加它。这并没有解决我的问题。
    protected void Page_Load(object sender, EventArgs e)
        {
            //you will get this, when any error will occur while authorization otherwise null    
            string Error = Request.QueryString["error"];
            //authorization code after successful authorization    
            string Code = Request.QueryString["code"];
            if (Error != null) { }
            else if (Code != null)
            {
                //Remember, we have set userid in State    
                string UserId = Request.QueryString["state"];
                //Get AccessToken    
                int Id = Convert.ToInt32(UserId);
                string AccessToken = string.Empty;
                string RefreshToken = ExchangeAuthorizationCode(Id, Code, out AccessToken);
                //saving refresh token in database    
                SaveRefreshToken(Id, RefreshToken);
                //Get Email Id of the authorized user    
                string EmailId = FetchEmailId(AccessToken);
                //Saving Email Id    
                SaveEmailId(UserId, EmailId);
                //Redirect the user to Authorize.aspx with user id    
                string Url = "Authorize.aspx?UserId=" + UserId;
                Response.Redirect(Url, true);
            }
        }
    
        private string ExchangeAuthorizationCode(int userId, string code, out string accessToken)
        {
            string baseurl = "https://accounts.google.com/o/oauth2/token";
            accessToken = string.Empty;
            string ClientSecret = ConfigurationManager.AppSettings["ClientSecrete"];
            string ClientId = ConfigurationManager.AppSettings["ClientId"];
            // //get this value by opening your web app in browser.    
            string RedirectUrl = "http://localhost:64716/GoogleCallback.aspx"; //I changed this to match the one in Postman
    
            using (var client = new HttpClient())
            {
                //client.BaseAddress = new Uri("https://accounts.google.com/o/oauth2"); //I replaced the Uri to "var result = client.PostAsync("https://accounts.google.com/o/oauth2/token", nvc).Result;"
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // I made this JSON
                // I removed this "client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");"
    
                var nvc = new FormUrlEncodedContent(new[] {
                new KeyValuePair<string, string>("grant_type", "authorization_code"),
                new KeyValuePair<string, string>("code", code),
                new KeyValuePair<string, string>("client_id", ClientId),
                new KeyValuePair<string, string>("client_secret", ClientSecret),
                new KeyValuePair<string, string>("redirect_uri", RedirectUrl),
                new KeyValuePair<string, string>("access_type", "offline")
                });
    
                var result = client.PostAsync("https://accounts.google.com/o/oauth2/token", nvc).Result;
    
                var resultContent = result.Content.ReadAsStringAsync();
    
                var tokenResponse = JsonConvert.DeserializeObject<Authorization_request_body>(resultContent.ToString());
    
                return "temp";
    
                }
    
            }