Today i was trying to integrate my Token Authentication with the rest of my website but when i tested the code it did not work. I know it worked before and i have not changed anything about it so what happened?
Besides Twitter i'm having the same problem with my code for Facebook and LinkedIn Authentication both of witch also just worked fine before???
Starting from the beginning i went to the wiki page of Tweetinvi: https://github.com/linvi/tweetinvi/wiki/Authentication And i copied the code Testing and debugging it.
First i had the problem of the (token = new AuthenticationToken) in the ValidateTwitterAuth ActionResult being null but i fixed that following the instruction's which worked. But then the next problem was that ITwitterCredentials userCreds is null and i have no idea why?
How do i fix this? Is the cause of this problem the same cause which stops my other Token handlers from working?
My Code:
(Part of Home Controller)
(I did remove the app-id and app-secret but i checked those so they er not the problem)
private IAuthenticationContext _authenticationContext;
// Step 1 : Redirect user to go on Twitter.com to authenticate
public ActionResult TwitterAuth()
{
var appCreds = new ConsumerCredentials("APP-ID", "APP-SECRET");
// Specify the url you want the user to be redirected to
var redirectURL = "http://" + Request.Url.Authority +"/Home/ValidateTwitterAuth";
_authenticationContext = AuthFlow.InitAuthentication(appCreds, redirectURL);
return new RedirectResult(_authenticationContext.AuthorizationURL);
}
public ActionResult ValidateTwitterAuth()
{
var token = new AuthenticationToken()
{
AuthorizationKey = "APP-ID",
AuthorizationSecret = "APP-SECRET",
ConsumerCredentials = new ConsumerCredentials("APP-ID", "APP-SECRET")
};
// Get some information back from the URL
var verifierCode = Request.Params.Get("oauth_verifier");
// Create the user credentials
ITwitterCredentials userCreds = AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, token);
// Do whatever you want with the user now!
ViewBag.User = Tweetinvi.User.GetAuthenticatedUser(userCreds);
string access_token = userCreds.AccessToken;
string access_token_secret = userCreds.AccessTokenSecret;
return RedirectToAction("Index", "Home");
}
Thanks in advance
Your problem comes from the following :
var token = new AuthenticationToken()
{
AuthorizationKey = "APP-ID",
AuthorizationSecret = "APP-SECRET",
};
The AuthorizationKey
and AuthorizationSecret
are not the APP_ID
and APP_SECRET
.
You should get these information from your _authenticationContext
created in your ActionResult TwitterAuth()
.
There are multiple way to store this information so that you can access it when the callback is done.
If you are not using load balancing I would suggest the following code that uses the url parameters :
public ActionResult TwitterAuth()
{
var appCreds = new ConsumerCredentials(MyCredentials.CONSUMER_KEY, MyCredentials.CONSUMER_SECRET);
var redirectURL = "http://" + Request.Url.Authority + "/Home/ValidateTwitterAuth";
var authenticationContext = AuthFlow.InitAuthentication(appCreds, redirectURL);
return new RedirectResult(authenticationContext.AuthorizationURL);
}
public ActionResult ValidateTwitterAuth()
{
var verifierCode = Request.Params.Get("oauth_verifier");
var authorizationId = Request.Params.Get("authorization_id");
if (verifierCode != null)
{
var userCreds = AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, authorizationId);
var user = Tweetinvi.User.GetAuthenticatedUser(userCreds);
ViewBag.User = user;
}
return View();
}
You can read more about this subject here : https://github.com/linvi/tweetinvi/wiki/Authentication#web-application-considerations.