我正在我的网站上实施谷歌登录。使用asspsnippets.dll。
我在页面加载的这一行中没有将对象引用设置为实例错误
string json = GoogleConnect.Fetch("me", code);
我的完整代码在这里
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPSnippets.GoogleAPI;
using System.Web.Script.Serialization;
namespace AdventureSports.Public
{
public partial class google_login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GoogleConnect.ClientId = "<My client Id>";
GoogleConnect.ClientSecret = "<My secret code>";
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);
lblId.Text = profile.Id;
lblName.Text = profile.DisplayName;
lblEmail.Text = profile.Emails.Find(email => email.Type == "account").Value;
lblGender.Text = profile.Gender;
lblType.Text = profile.ObjectType;
ProfileImage.ImageUrl = profile.Image.Url;
pnlProfile.Visible = true;
btnLogin.Enabled = false;
}
if (Request.QueryString["error"] == "access_denied")
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
}
}
protected void Login(object sender, EventArgs e)
{
GoogleConnect.Authorize("profile", "email");
}
protected void Clear(object sender, EventArgs e)
{
GoogleConnect.Clear();
}
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; }
}
}
}
答案 0 :(得分:0)
确保您有一个名为&#39; code&#39;的查询字符串参数。 如果querystring参数具有空值,则通常会发生此错误。请输入断点并检查查询字符串参数的值&#39;代码&#39;。希望该值未正确分配给查询字符串参数&#39;代码&#39;
答案 1 :(得分:0)
dll只有问题。从下面的url下载新的dll并参考此示例代码