首页 > 解决方案 > 无法将“System.String”类型的对象转换为“?2?.?3?.?5?”类型

问题描述

从我的 ASP.NET 网页调用 Google 登录时,我遇到了这个错误。

这是我在执行代码时遇到的错误。

System.InvalidOperationException: Cannot convert object of type 'System.String' to type '?2?.?3?.?5?'

我正在使用带有 C# 的 ASP.NET 将 Google 登录集成到我的网页。

我的代码如下:

page_load() {

        GoogleConnect.ClientId = "xxxxxxxx.apps.googleusercontent.com";
        GoogleConnect.ClientSecret = "xxxxxxxxxx";
        GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];

        if (!string.IsNullOrEmpty(Request.QueryString["code"]))
        {
            string code = Request.QueryString["code"];
            string json = GoogleConnect.Fetch("me", "code");
            GoogleProfile profile = new JavaScriptSerializer().Deserialize<GoogleProfile>(json);
            Label1.Text = profile.Id;
            Label2.Text = profile.DisplayName;
            Label3.Text = profile.Emails.Find(email => email.Type == "account").Value;
            Image1.ImageUrl = profile.Image.Url;
            Button1.Visible = false;
        }
        if (Request.QueryString["error"] == "access_denied")
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        GoogleConnect.Authorize("profile", "email");
    }

    public class GoogleProfile
    {

        public string Id { get; set; }
        public string DisplayName { get; set; }
        public Image Image { get; set; }
        public List<Email> Emails { get; set; }
        public string Gender { get; set; }
        public string ObjectType { get; set; }
    }

    public class Email
    {
        public string Value { get; set; }
        public string Type { get; set; }
    }

    public class Image
    {
        public string Url { get; set; }
    }
}

标签: c#asp.netgoogle-oauth

解决方案


推荐阅读