Last active
January 2, 2016 07:49
-
-
Save Elrashid/28be7df7d6dc60470a01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Windows.Data.Json; | |
using Windows.Security.Authentication.Web; | |
using Windows.Web.Http; | |
namespace WabCS | |
{ | |
class WebAuthenticationBroker_Facebook_Utility | |
{ | |
public class FaceBook | |
{ | |
public string access_token { get; set; } | |
public string expires_in { get; set; } | |
public string Person_Name { get; set; } | |
} | |
static public async Task<FaceBook> Authentication() | |
{ | |
String FacebookClientIDText = ""; | |
String FacebookCallbackUrlText = ""; | |
String FacebookURL = ""; | |
FacebookURL += "https://www.facebook.com/dialog/oauth?client_id="; | |
FacebookURL += Uri.EscapeDataString(FacebookClientIDText); | |
FacebookURL += "&redirect_uri="; | |
FacebookURL += Uri.EscapeDataString(FacebookCallbackUrlText); | |
FacebookURL += "&scope=read_stream&display=popup&response_type=token"; | |
System.Uri StartUri = new Uri(FacebookURL); | |
System.Uri EndUri = new Uri(FacebookCallbackUrlText); | |
FaceBook fb = await AuthenticateAsync(StartUri, EndUri); | |
return fb; | |
} | |
static private async Task<FaceBook> AuthenticateAsync(System.Uri StartUri, | |
System.Uri EndUri) | |
{ | |
try | |
{ | |
string rowData = await GetHttpResponseAsync(StartUri, EndUri); | |
FaceBook fb = Deserialize(rowData); | |
fb.Person_Name = await GetPersonNameAsync(fb); | |
return fb; | |
} | |
catch (Exception exp) | |
{ | |
// exp.Message | |
return new FaceBook(); | |
} | |
} | |
static private async Task<string> GetHttpResponseAsync(System.Uri StartUri, | |
System.Uri EndUri) | |
{ | |
WebAuthenticationResult WebAuthenticationResult = | |
await WebAuthenticationBroker. | |
AuthenticateAsync(WebAuthenticationOptions.None, | |
StartUri, | |
EndUri); | |
if (WebAuthenticationResult.ResponseStatus == | |
WebAuthenticationStatus.Success) | |
{ | |
return WebAuthenticationResult.ResponseData.ToString(); | |
} | |
else if (WebAuthenticationResult.ResponseStatus == | |
WebAuthenticationStatus.ErrorHttp) | |
{ | |
// WebAuthenticationResult.ResponseErrorDetail.ToString() | |
return String.Empty; | |
} | |
else | |
{ | |
//WebAuthenticationResult.ResponseStatus.ToString() | |
return String.Empty; | |
} | |
} | |
static private FaceBook Deserialize(string webAuthResultResponseData) | |
{ | |
//Get Access Token first | |
string responseData = webAuthResultResponseData.Substring( | |
webAuthResultResponseData.IndexOf("access_token")); | |
String[] keyValPairs = responseData.Split('&'); | |
string access_token = null; | |
string expires_in = null; | |
for (int i = 0; i < keyValPairs.Length; i++) | |
{ | |
String[] splits = keyValPairs[i].Split('='); | |
switch (splits[0]) | |
{ | |
case "access_token": | |
access_token = splits[1]; | |
break; | |
case "expires_in": | |
expires_in = splits[1]; | |
break; | |
} | |
} | |
return new FaceBook() { access_token = access_token, | |
expires_in = expires_in }; | |
} | |
static private async Task<string> GetPersonNameAsync(FaceBook fb) | |
{ | |
HttpClient httpClient = new HttpClient(); | |
string response = await httpClient.GetStringAsync( | |
new Uri("https://graph.facebook.com/me?access_token=" | |
+ fb.access_token)); | |
JsonObject value = JsonValue.Parse(response).GetObject(); | |
string facebookUserName = value.GetNamedString("name"); | |
return facebookUserName; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment