Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

(Difference between revisions)

Authenticating to Social Services using Infragistics

From Wiki
Jump to: navigation, search
rgunawans (Talk | contribs)
(Rgunawans - - Introduction)
rgunawans (Talk | contribs)
(Rgunawans -)
Line 1: Line 1:
[[Category:Draft]]
+
[[Category:Windows Phone 8]][[Category:Tools for Windows Phone]][[Category:Windows Phone 7.x]]
 
{{Abstract|This article explains how to use  [http://www.infragistics.com/products/windows-phone/features/access-control/ Infragistics Access Control], this control makes easier to get authenticate to Twitter, Facebook, LiveID and Google services}} '
 
{{Abstract|This article explains how to use  [http://www.infragistics.com/products/windows-phone/features/access-control/ Infragistics Access Control], this control makes easier to get authenticate to Twitter, Facebook, LiveID and Google services}} '
  

Revision as of 17:23, 27 January 2014

This article explains how to use Infragistics Access Control, this control makes easier to get authenticate to Twitter, Facebook, LiveID and Google services '

WP Metro Icon Tools.png
WP Metro Icon WP8.png
Article Metadata
Tested with
SDK: Windows Phone 8.0 SDK
Devices(s): Tested on Lumia 720
Compatibility
Platform(s):
Windows Phone 8
Article
Created: (25 Jan 2014)
Last edited: rgunawans (27 Jan 2014)

Introduction

As a developer, sometimes we need to share our work to social media or if we want to develop an application that need Soc Med services, we have to create authentication to that services by our self. First of all, before we use Twitter/Facebook/Live ID or Google, make sure that we have application key.

The table below authentication providers and URL to register an application

Provider Name URL
Facebook https://developers.facebook.com/apps
Twitter https://dev.twitter.com/apps/new
Live ID https://manage.dev.live.com/AddApplication.aspx
Google https://code.google.com/apis/console/?pli=1

I will show how to use the Infragistics Access Control.

First, create new Project, choose Windows Phone App, enter the name of project

create new project

Find "XamAccessControl" in the toolbox and drag to MainPage.xaml

AccessControl

Setting the control like my code to use Twitter Authentication <ig:XamAccessControl x:Name="accessCtrl" HorizontalAlignment="Left" Height="100" Margin="37,227,0,0" VerticalAlignment="Top" Width="100" >

               <ig:XamAccessControl.AuthenticationService>
                   <ig:AuthenticationService
                       Provider="Twitter"
                       ClientId="xxxxxx"
                       RedirectUri="http://www.dvlup.com"
                       ClientSecret="xxxxx"                        />
                   
               </ig:XamAccessControl.AuthenticationService>
           </ig:XamAccessControl>


Please note, the "Provider" property can be change to

  1. Twitter
  2. Facebook
  3. Live Id
  4. Google

and drag a button to activate authentication process and put below code:

private void Button_Click(object sender, RoutedEventArgs e)
{
accessTwitter.IsOpen = true;
}

Next step is "Authentication Event Handling", you can use below code after "InitializeComponent()"

public MainPage()
{
InitializeComponent();
 
accessTwitter.AuthenticationCompleted += new EventHandler<Infragistics.Controls.Authentication.AuthenticationCompletedEventArgs>(accessTwitter_AuthenticationCompleted);
accessTwitter.AuthenticationDenied += new EventHandler<Infragistics.Controls.Authentication.AuthenticationDeniedEventArgs>(accessTwitter_AuthenticationDenied);
accessTwitter.AuthenticationException += new EventHandler<Infragistics.Controls.Authentication.AuthenticationExceptionEventArgs>(accessTwitter_AuthenticationException);
 
}
 
void accessTwitter_AuthenticationException(object sender, Infragistics.Controls.Authentication.AuthenticationExceptionEventArgs e)
{
Debug.WriteLine("AUTHENTICATION_EXCEPTION:" + e.Exception.Message);
}
 
void accessTwitter_AuthenticationDenied(object sender, Infragistics.Controls.Authentication.AuthenticationDeniedEventArgs e)
{
Debug.WriteLine("AUTHENTICATION_DENIED:" + e.AuthenticationDeniedResult.Error);
}
 
void accessTwitter_AuthenticationCompleted(object sender, Infragistics.Controls.Authentication.AuthenticationCompletedEventArgs e)
{
Debug.WriteLine("AUTHENTICATION_COMPLETED:" + e.AuthenticationResult.AuthenticationToken);
}

Done.. that's it, it's easy to get authentication token from Providers, less than 5 minutes coding..

Summary

In order to use below Authentication APIs

  1. Facebook
  2. Twitter
  3. Live ID
  4. Google

you can use Infragistics Access Control, it more easy than you create by yourself. Below is comparison steps between Infragistics Access Control and code that I've use already

Add categories below using category selector.
Infragistics Access Control Manual Code
Drag Access Control Create new Page, name it TwitterAuthPage.xaml
Set the provider and App ID and App Secret Drag the browser to TwitterAuthPage.xaml
Drag a button Put the code for browser access OnNavigate
Activate the control "accessTwitter.IsOpen = true;" Put the code
private void GetTwitterToken()
{
var credentials = new OAuthCredentials
{
Type = OAuthType.RequestToken,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
ConsumerKey = TwitterSettings.ConsumerKey,
ConsumerSecret = TwitterSettings.ConsumerKeySecret,
Version = TwitterSettings.OAuthVersion,
CallbackUrl = TwitterSettings.CallbackUri
};
var client = new RestClient
{
Authority = "https://api.twitter.com/oauth",
Credentials = credentials,
HasElevatedPermissions = true,
SilverlightAcceptEncodingHeader = "gizp",
DecompressionMethods = DecompressionMethods.GZip,
};
 
var request = new RestRequest
{
Path = "/request_token"
};
client.BeginRequest(request, new RestCallback(TwitterRequestTokenCompleted));
}
private void TwitterRequestTokenCompleted(RestRequest request, RestResponse response, object userstate)
{
_oAuthToken = GetQueryParameter(response.Content, "oauth_token");
_oAuthTokenSecret = GetQueryParameter(response.Content, "oauth_token_secret");
 
var authorizeUrl = TwitterSettings.AuthorizeUri + "?oauth_token=" + _oAuthToken;
if (String.IsNullOrEmpty(_oAuthToken) || String.IsNullOrEmpty(_oAuthTokenSecret))
{
Dispatcher.BeginInvoke(() => MessageBox.Show("error calling twitter"));
return;
}
Dispatcher.BeginInvoke(() => BrowserControl.Navigate(new Uri(authorizeUrl)));
}
private static string GetQueryParameter(string input, string parameterName)
{
foreach (string item in input.Split('&'))
{
var parts = item.Split('=');
if (parts[0] == parameterName)
{
return parts[1];
}
}
return String.Empty;
}
private void BrowserControl_Navigated(object sender, NavigationEventArgs e)
{
ProgressBar.IsIndeterminate = false;
ProgressBar.Visibility = Visibility.Collapsed;
}
private void BrowserControl_Navigating(object sender, NavigatingEventArgs e)
{
ProgressBar.IsIndeterminate = true;
ProgressBar.Visibility = Visibility.Visible;
if (e.Uri.AbsoluteUri.CompareTo("https://api.twitter.com/oauth/authorize") == 0)
{
ProgressBar.IsIndeterminate = true;
ProgressBar.Visibility = Visibility.Visible;
}
if (!e.Uri.AbsoluteUri.Contains(TwitterSettings.CallbackUri))
return;
e.Cancel = true;
var arguments = e.Uri.AbsoluteUri.Split('?');
if (arguments.Length < 1)
return;
GetAccessToken(arguments[1]);
}
private void GetAccessToken(string uri)
{
var requestToken = GetQueryParameter(uri, "oauth_token");
if (requestToken != _oAuthToken)
{
MessageBox.Show("Twitter auth tokens don't match");
}
var requestVerifier = GetQueryParameter(uri, "oauth_verifier");
var credentials = new OAuthCredentials
{
Type = OAuthType.AccessToken,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
ConsumerKey = TwitterSettings.ConsumerKey,
ConsumerSecret = TwitterSettings.ConsumerKeySecret,
Token = _oAuthToken,
TokenSecret = _oAuthTokenSecret,
Verifier = requestVerifier
};
var client = new RestClient
{
Authority = "https://api.twitter.com/oauth",
Credentials = credentials,
HasElevatedPermissions = true
};
var request = new RestRequest
{
Path = "/access_token"
};
client.BeginRequest(request, new RestCallback(RequestAccessTokenCompleted));
}
private void RequestAccessTokenCompleted(RestRequest request, RestResponse response, object userstate)
{
var twitteruser = new TwitterAccess
{
AccessToken = GetQueryParameter(response.Content, "oauth_token"),
AccessTokenSecret = GetQueryParameter(response.Content, "oauth_token_secret"),
UserId = GetQueryParameter(response.Content, "user_id"),
ScreenName = GetQueryParameter(response.Content, "screen_name")
};
if (String.IsNullOrEmpty(twitteruser.AccessToken) || String.IsNullOrEmpty(twitteruser.AccessTokenSecret))
{
Dispatcher.BeginInvoke(() => MessageBox.Show(response.Content));
return;
}
Helper.SaveSetting(Constants.TwitterAccess, twitteruser);
IsolatedStorageSettings mysetting = IsolatedStorageSettings.ApplicationSettings;
mysetting.Remove("twitter_token");
mysetting.Remove("twitter_tokensecret");
 
mysetting.Add("twitter_token", twitteruser.AccessToken);
mysetting.Add("twitter_tokensecret", twitteruser.AccessTokenSecret);
 
mysetting.Save();
// App.keluar = false;
sukses = 1;
Dispatcher.BeginInvoke(() =>
{
if (NavigationService.CanGoBack)
{
NavigationService.GoBack();
}
});
}
Configure Event Handling set the event to navigate TwitterAuthPage.xaml
Get the Authentication Token Drag button to MainPage.xaml
Done Put event on the button di navigate to TwitterAuthPage.xaml
97 page views in the last 30 days.
×