rexroxm
rexroxm

Reputation: 878

GoogleConnect.Fetch throws Unable to cast object error

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.

Upvotes: 3

Views: 743

Answers (1)

Naveen
Naveen

Reputation: 294

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

Upvotes: 3

Related Questions