Hi everyone,
I need to create a simple "Log in" with Twitter account and I have the following problems:
And B) How to get user infos, in /TwitterCallbak.aspx the line
Cheers
I need to create a simple "Log in" with Twitter account and I have the following problems:
-
A) Authorize "to use your account?" keep appearing - steps are:
1 - Create a Twitter account -> Create an new App -> Permission: "Read only" and select checkbox "Allow this application to be used to Sign in with Twitter"
2 - From my page /TwitterLogin.aspx -> https://api.twitter.com/oauth/authorize?oauth_token={TOKEN} -> After I click accept "Authorize "<APP_NAME>" to use your account?" then I'm redirected to callback page -> /TwitterCallbak.aspx get the "oauth_token" and "oauth_verifier"
3 - So at this point I've approve that app to use my twitter account. If I repeat the process - access /TwitterLogin.aspx the app request to be approved again and again.
protected void Page_Load(object sender, EventArgs e)
{
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials("<My_ConsumerKey>", "<My_ConsumerSecret>");
string url = CredentialsCreator.GetAuthorizationURLForCallback(applicationCredentials, string.Format("{0}/SocialLogin/TwitterCallbak.aspx", Utils.GetSiteRootUrl()));
//Saving applicationCredentials into session for using them later.
Session["AUTH-KEY"] = applicationCredentials.AuthorizationKey;
Session["AUTH-SECRET"] = applicationCredentials.AuthorizationSecret;
Response.Redirect(url);
}
Code for /TwitterCallbak.aspxprotected void Page_Load(object sender, EventArgs e)
{
//OAuth Tokens
string OAuthTokenCode = Utils.Get<string>("oauth_token", string.Empty);
string OAuthVerifierCode = Utils.Get<string>("oauth_verifier", string.Empty);
if (!string.IsNullOrEmpty(OAuthTokenCode) && !string.IsNullOrEmpty(OAuthVerifierCode))
{
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials("<My_ConsumerKey>", "<My_ConsumerSecret>");
//Setting applicationCredentials to the previous one i saved in sessions.
applicationCredentials.AuthorizationKey = Session["AUTH-KEY"].ToString();
applicationCredentials.AuthorizationSecret = Session["AUTH-SECRET"].ToString();
// Here we provide the entire URL where the user has been redirected
try
{
var newCredentials = CredentialsCreator.GetCredentialsFromVerifierCode(OAuthVerifierCode, applicationCredentials);
if (newCredentials != null)
{
//use your credentials here newCredentials.AccessToken, newCredentials.AccessTokenSecret
string AccessToken = newCredentials.AccessToken;
string AccessTokenSecret = newCredentials.AccessTokenSecret;
TwitterCredentials.SetCredentials(AccessToken, AccessTokenSecret, "<My_ConsumerKey>", "<My_ConsumerSecret>");
var user = Tweetinvi.User.GetLoggedUser();
//log success
}
}
catch (Exception ex)
{
//log error
}
return;
}
//CANCEL/DENY APP REQUEST
string UserDenyCode = Utils.Get<string>("denied", string.Empty);
if (!string.IsNullOrEmpty(UserDenyCode))
{
//log error
return;
}
}
After some research I found out that there are two URLs used in auth process:https://api.twitter.com/oauth/authenticate?oauth_token={TOKEN}so I've saw that /TwitterLogin.aspx generate a url like this one
https://api.twitter.com/oauth/authorize?oauth_token={TOKEN}
https://api.twitter.com/oauth/authorize?oauth_token={TOKEN}and to resolve the problem in /TwitterLogin.aspx I've replace the line
string url = CredentialsCreator.GetAuthorizationURLForCallback(applicationCredentials, string.Format("{0}/SocialLogin/TwitterCallbak.aspx", Utils.GetSiteRootUrl()));
withstring url = CredentialsCreator.GetAuthorizationURLForCallback(applicationCredentials, string.Format("{0}/SocialLogin/TwitterCallbak.aspx", Utils.GetSiteRootUrl())).Replace("authorize", "authenticate");
And now the problem seams to go away, but the question is there another way to do this and I miss it ?And B) How to get user infos, in /TwitterCallbak.aspx the line
var user = Tweetinvi.User.GetLoggedUser();
return all properties with NULL values, so how can I get Twitter Username, ScreenName, Profile Image please ?Cheers