GoogleConnect.Fetch throws Unable to cast object error

安稳与你 提交于 2021-02-05 05:51:05

问题


I am integrating the Google API into my project. I am using the following code to do so:

public void google()
    {

        GoogleConnect.ClientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        GoogleConnect.ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];

        if (!string.IsNullOrEmpty(Request.QueryString["code"]))
        {
            string code = Request.QueryString["code"].ToString();
            string json = GoogleConnect.Fetch("me", code.ToString());
            GoogleProfile profile = new JavaScriptSerializer().Deserialize<GoogleProfile>(json);
            //code for showing data on my page
        }
        if (Request.QueryString["error"] == "access_denied")
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
        }

    }
    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; }
    }

It runs when I click on the google button and shows data on the first instance but it throws the following error the second time I try it.

Server Error in '/' Application. Unable to cast object of type 'System.String' to type '?1?'.

I also have a facebook and twitter button on the same screen but I doubt that it is causing the problem. Any suggestions will be helpful.


回答1:


i have used this api you should used it place a clear button where you show profile create button clear

 protected void Clear(object sender, EventArgs e)
    {
        GoogleConnect.Clear();
    }


来源:https://stackoverflow.com/questions/42992739/googleconnect-fetch-throws-unable-to-cast-object-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!