我正在尝试从 Hotmail 导入联系人。为此,我正在使用以下代码
private const string wlCookie = "wl_auth";
private const string clientId = "...clientId...";
private const string callback = "http://localhost:8465/";
private const string clientSecret = "...secret key...";
private const string oauthUrl = "https://oauth.live.com/token";
public ActionResult Index(string code)
{
if (code != null)
{
GetMsnContacts(Request.QueryString["code"].ToString());
}
return View();
}
private void GetMsnContacts(string code)
{
MsnImport msnImport = new MsnImport();
List<string> emaillist = new List<string>();
emaillist = msnImport.GetMsnContacts(code);
}
msnImport.cs
public List<string> GetMsnContacts(string code)
{
try
{
HttpContext context = HttpContext.Current;
string verifier = code;
OAuthToken token;
OAuthError error;
if (!string.IsNullOrEmpty(verifier))
{
RequestAccessTokenByVerifier(verifier, out token, out error);
}
}
catch (Exception ex) { }
return emaillist;
}
private void RequestAccessTokenByVerifier(string verifier, out OAuthToken token, out OAuthError error)
{
string content = String.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code",
HttpUtility.UrlEncode(clientId),
HttpUtility.UrlEncode(Callback),
HttpUtility.UrlEncode(clientSecret),
HttpUtility.UrlEncode(verifier));
RequestAccessToken(content, out token, out error);
}
private void RequestAccessToken(string postContent, out OAuthToken token, out OAuthError error)
{
token = null;
error = null;
HttpWebRequest request = WebRequest.Create(oauthUrl) as HttpWebRequest;
request.Method = "POST";
try
{
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(postContent);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response != null)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OAuthToken));
var objResponse = response.GetResponseStream();
token = serializer.ReadObject(response.GetResponseStream()) as OAuthToken;
if (token != null)
{
RequestContacts(token.AccessToken);
}
}
}
catch (Exception ex)
{
}
}
token = serializer.ReadObject(response.GetResponseStream()) as OAuthToken; 这给出了一个错误。
{System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type ContactsImport.Helper.OAuthToken. Encountered unexpected character '<'. ---> System.Xml.XmlException: Encountered unexpected character '<'.
at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, XmlException exception)
at System.Runtime.Serialization.Json.XmlJsonReader.ReadAttributes()
at System.Runtime.Serialization.Json.XmlJsonReader.ReadNonExistentElementName(StringHandleConstStringType elementName)
at System.Runtime.Serialization.Json.XmlJsonReader.Read()
at System.Xml.XmlBaseReader.IsStartElement()
at System.Xml.XmlBaseReader.IsStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
at System.Runtime.Serialization.XmlReaderDelegator.IsStartElement(XmlDictionaryString localname, XmlDictionaryString ns)
at System.Runtime.Serialization.XmlObjectSerializer.IsRootElement(XmlReaderDelegator reader, DataContract contract, XmlDictionaryString name, XmlDictionaryString ns)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalIsStartObject(XmlReaderDelegator reader)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
--- End of inner exception stack trace ---
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)
at ContactsImport.Helper.MsnImport.RequestAccessToken(String postContent, OAuthToken& token, OAuthError& error)
我们如何解决这个错误?