Solve NullValueException handled by user code

Oct 12 2011 7:30 AM
Hello

Below is the code that I am using. I want to retrieve the userid and name from collection. Can anyone tell me what is correct method of retrieve id and name from the NameValueCollection.

Collapse | Copy Code

private void HandleAuthorizeTokenResponse()
{
string consumerKey = ConfigurationManager.AppSettings["googleConsumerKey"];
string consumerSecret = ConfigurationManager.AppSettings["googleConsumerSecret"];
string token = Request.QueryString["oauth_token"];
string verifier = Request.QueryString["oauth_verifier"];
string accessTokenEndpoint = "https://www.google.com/accounts/OAuthGetAccessToken";

// Exchange the Request Token for an Access Token
var oAuthConsumer = new OAuthConsumerNew();

var accessToken = oAuthConsumer.GetOAuthAccessToken(accessTokenEndpoint, realm, consumerKey, consumerSecret, token, verifier, GetRequesttoken().TokenSecret);

// Google Only - This method will get the email of the authenticated user
var responseText = oAuthConsumer.GetUserInfo("https://www.googleapis.com/oauth2/v1/userinfo?alt=json", realm, consumerKey, consumerSecret, accessToken.Token, accessToken.TokenSecret);
string queryString = responseText;

NameValueCollection nvc = StringToValue(queryString.Replace("\"", ""));
string userid = string.Empty;
string name = string.Empty;

if (nvc["id"] != "")
{
userid = nvc["id"].ToString();
// NullValueException handled by user code
name = nvc["name"].ToString();
// NullValueException handled by user code
Response.Write("Userid" + userid + "<br />");
Response.Write("FName=" + name + "<br />");
}
}


NameValueCollection StringToValue(string queryString)
{
NameValueCollection queryParameters = new NameValueCollection();
string[] querySegments = queryString.Split(',');
foreach (string segment in querySegments)
{
string[] parts = segment.Split(':');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();
Response.Write(key + ":" + val);
queryParameters.Add(key, val);
}
}
return queryParameters;
}



Thanks,
Deepak

Answers (3)