GoogleConnect.Fetch引发无法投射对象错误 [英] GoogleConnect.Fetch throws Unable to cast object error

查看:53
本文介绍了GoogleConnect.Fetch引发无法投射对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Google API集成到我的项目中.我正在使用以下代码来做到这一点:

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

它在我单击google按钮时运行,并在第一个实例上显示数据,但是第二次尝试时会引发以下错误.

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.

"/"应用程序中的服务器错误.无法转换类型的对象"System.String"键入?1?".

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

我在同一屏幕上也有一个facebook和twitter按钮,但我怀疑这是引起问题的原因.任何建议都会有所帮助.

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.

推荐答案

我使用过此api,应该使用它在显示个人资料的地方放置一个清除按钮清除创建按钮

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

这篇关于GoogleConnect.Fetch引发无法投射对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆