我已经完成了这个实现,引用了另一个代码..我的代码是这样的..
#region web query response methods
/// <summary>
/// Event that initiates QueryResponse for RequestToken request
/// </summary>
/// <param name="sender"></param>
/// <param name="e">Webresponse event argument</param>
void requestTokenQuery_QueryResponse(object sender, WebQueryResponseEventArgs e)
{
try
{
var parameters = TwitterHelperMethods.GetQueryParameters(e.Response);
OAuthTokenKey = parameters["oauth_token"];
tokenSecret = parameters["oauth_token_secret"];
var authorizeUrl = TwitterSettings.AuthorizeUri + "?oauth_token=" + OAuthTokenKey;
Dispatcher.BeginInvoke(() =>
{
this.browseTwitter.Navigate(new Uri(authorizeUrl));
});
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(ex.Message);
});
}
}
/// <summary>
/// Event that initiates QueryResponse for AccessToken request
/// </summary>
/// <param name="sender"></param>
/// <param name="e">Webresponse event argument</param>
void AccessTokenQuery_QueryResponse(object sender, WebQueryResponseEventArgs e)
{
try
{
var parameters = TwitterHelperMethods.GetQueryParameters(e.Response);
accessToken = parameters["oauth_token"];
accessTokenSecret = parameters["oauth_token_secret"];
userID = parameters["user_id"];
userScreenName = parameters["screen_name"];
TwitterHelperMethods.SetKeyValue<string>("AccessToken", accessToken);
TwitterHelperMethods.SetKeyValue<string>("AccessTokenSecret", accessTokenSecret);
TwitterHelperMethods.SetKeyValue<string>("ScreenName", userScreenName);
cacheManager.SaveToIsolatedStorage(Utilities.TwitterID, userID);
cacheManager.SaveToIsolatedStorage(Utilities.TwitterAccessToken, accessToken);
cacheManager.SaveToIsolatedStorage(Utilities.TwitterSecretAccessToken, accessTokenSecret);
// NavigationService.Navigate(new Uri("/HomePage.xaml", UriKind.RelativeOrAbsolute));
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(ex.Message);
});
}
}
#endregion
#region Browser events methods
/// <summary>
/// Called after the broswer is loaded
/// </summary>
/// <param name="sender">Browser</param>
/// <param name="e">Routed Event arguments</param>
private void browseTwitter_Loaded(object sender, RoutedEventArgs e)
{
accessToken = TwitterHelperMethods.GetKeyValue<string>("AccessToken");
accessTokenSecret = TwitterHelperMethods.GetKeyValue<string>("AccessTokenSecret");
userScreenName = TwitterHelperMethods.GetKeyValue<string>("ScreenName");
if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(accessTokenSecret))
{
var requestTokenQuery = TwitterOAuthHelper.GetRequestTokenQuery();
requestTokenQuery.RequestAsync(TwitterSettings.RequestTokenUri, null);
requestTokenQuery.QueryResponse += new EventHandler<WebQueryResponseEventArgs>(requestTokenQuery_QueryResponse);
}
}
/// <summary>
/// Called when browser Initiates Navigation to Uri provided
/// </summary>
/// <param name="sender">Browser</param>
/// <param name="e">Navigating event arguments</param>
private void browseTwitter_Navigating(object sender, NavigatingEventArgs e)
{
if (e.Uri.ToString().StartsWith(TwitterSettings.CallbackUri))
{
cacheManager = new CacheManager();
var AuthorizeResult = TwitterHelperMethods.GetQueryParameters(e.Uri.ToString());
var VerifyPin = AuthorizeResult["oauth_verifier"];
//We now have the Verification pin
//Using the request token and verification pin to request for Access tokens
var AccessTokenQuery = TwitterOAuthHelper.GetAccessTokenQuery(
OAuthTokenKey, //The request Token
tokenSecret, //The request Token Secret
VerifyPin // Verification Pin
);
AccessTokenQuery.QueryResponse += new EventHandler<WebQueryResponseEventArgs>(AccessTokenQuery_QueryResponse);
AccessTokenQuery.RequestAsync(TwitterSettings.AccessTokenUri, null);
NavigationService.Navigate(new Uri("/OtherPages/HomePage.xaml", UriKind.RelativeOrAbsolute));
}
}
#endregion
我的 Twitterhelpermethods 类是这样的
public class TwitterHelperMethods
{
public static Dictionary<string, string> GetQueryParameters(string response)
{
Dictionary<string, string> nameValueCollection = new Dictionary<string, string>();
string[] items = response.Split('&');
foreach (string item in items)
{
if (item.Contains("="))
{
string[] nameValue = item.Split('=');
if (nameValue[0].Contains("?"))
nameValue[0] = nameValue[0].Replace("?", "");
nameValueCollection.Add(nameValue[0], System.Net.HttpUtility.UrlDecode(nameValue[1]));
}
}
return nameValueCollection;
}
}
和 twitteroauthhelper.cs 是
public class TwitterOAuthHelper
{
public static OAuthWebQuery GetRequestTokenQuery()
{
var oauth = new OAuthWorkflow
{
ConsumerKey = TwitterSettings.consumerKey,
ConsumerSecret = TwitterSettings.consumerKeySecret,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
RequestTokenUrl = TwitterSettings.RequestTokenUri,
Version = TwitterSettings.oAuthVersion,
CallbackUrl = TwitterSettings.CallbackUri
};
var info = oauth.BuildRequestTokenInfo(WebMethod.Get);
var objOAuthWebQuery = new OAuthWebQuery(info);
objOAuthWebQuery.HasElevatedPermissions = true;
objOAuthWebQuery.SilverlightUserAgentHeader = "Hammock";
objOAuthWebQuery.SilverlightMethodHeader = "GET";
return objOAuthWebQuery;
}
public static OAuthWebQuery GetAccessTokenQuery(string requestToken, string RequestTokenSecret, string oAuthVerificationPin)
{
var oauth = new OAuthWorkflow
{
AccessTokenUrl = TwitterSettings.AccessTokenUri,
ConsumerKey = TwitterSettings.consumerKey,
ConsumerSecret = TwitterSettings.consumerKeySecret,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
Token = requestToken,
Verifier = oAuthVerificationPin,
Version = TwitterSettings.oAuthVersion
};
var info = oauth.BuildAccessTokenInfo(WebMethod.Post);
var objOAuthWebQuery = new OAuthWebQuery(info);
objOAuthWebQuery.HasElevatedPermissions = true;
objOAuthWebQuery.SilverlightUserAgentHeader = "Hammock";
objOAuthWebQuery.SilverlightMethodHeader = "GET";
return objOAuthWebQuery;
}
}
twittersettings.cs :-
public class TwitterSettings
{
public static string RequestTokenUri = "https://api.twitter.com/oauth/request_token";
public static string AuthorizeUri = "https://api.twitter.com/oauth/authorize";
public static string AccessTokenUri = "https://api.twitter.com/oauth/access_token";
public static string CallbackUri = "http://www.qwinixtech.com";
public static string StatusUpdateUrl { get { return "http://api.twitter.com"; } }
// #error TODO REGISTER YOUR APP WITH TWITTER TO GET YOUR KEYS AND FILL THEM IN HERE
public static string consumerKey = "Your consumer key here";
public static string consumerKeySecret = "Your consumer secret key here";
public static string AccessToken = "Your access token here";
public static string AccessTokenSecret = "Your Secret access token here";
public static string oAuthVersion = "1.0a";
}
这工作得很好..你需要做的只是吊床库..我希望你已经拥有它..否则请下载它..