· 9 years ago · Oct 02, 2016, 07:12 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Windows.Security.Authentication.Web;
7using Windows.Web.Http;
8using Windows.Data.Json;
9using System.IO;
10
11namespace ProjekatOoad.Services
12{
13 class FourSquareConnector
14 {
15 //OauthToken glavni potreban za Rest pozive, cuva autorizaciju da se moze obaviti poziv
16 private string oAuthToken;
17 //id i secret su potrebni da se dobije oauth
18 private string clientId;
19 private string clientSecret;
20 //na koju stranicu da se ide nakon zavrsene autorizacije
21 private string redirectUri;
22
23 // konstruktor prima objekat sa postavkama
24 public FourSquareConnector(JsonObject fourSquareSettings)
25 {
26 clientId = fourSquareSettings.GetNamedString("clientId");
27 clientSecret = fourSquareSettings.GetNamedString("clientSecret");
28 redirectUri = fourSquareSettings.GetNamedString("redirectUri");
29 }
30
31 public string OAuthToken
32 {
33 get
34 {
35 return oAuthToken;
36 }
37 set
38 {
39 oAuthToken = value;
40 }
41 }
42 //Cilj metode je da pronadje i postavi OAuthToken
43 public async Task authenticate()
44 {
45 try
46 {
47 //Sastavi url poziva
48 String foursqareApiUrl = "https://foursquare.com/oauth2/authenticate?client_id=" + clientId + "&response_type=code&redirect_uri=" + redirectUri;
49 Uri StartUri = new Uri(foursqareApiUrl);
50 //poziv authentifikacije, ovo ce otvorit forsquare web stranicu koja trazi od korisnika da se loguje
51 WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, StartUri, new Uri("https://www.google.ba/"));
52 //Ako je sve ok izvuci ce oauthtoken iz response
53 if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
54 {
55 await getOuathToken(WebAuthenticationResult.ResponseData.ToString());
56 }
57 //ako nije ok treba baciti exception
58 else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
59 {
60 throw new FourSquareAuthenticationException("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString());
61 }
62 else
63 {
64 throw new FourSquareAuthenticationException("Error returned by AuthenticiateAsync()" + WebAuthenticationResult.ResponseStatus.ToString());
65 }
66 }
67 catch (Exception Error)
68 {
69 throw Error;
70 }
71 }
72
73 private async Task getOuathToken(string webAuthResultResponseData)
74 {
75 //auth poziv dobije code parametar koji se zatim iskoristiti da se dobije oauth
76 //izdvajanje code parametra iz dobivenog stringa
77 string responseData = webAuthResultResponseData.Substring(webAuthResultResponseData.IndexOf("code"));
78 String[] keyValPairs = responseData.Split('&');
79 string code = null;
80 for (int i = 0; i < keyValPairs.Length; i++)
81 {
82 String[] splits = keyValPairs[i].Split('=');
83 if (splits[0].Equals("code"))
84 {
85 code = splits[1];
86 }
87 }
88 // Koristenje code za dobivanje access tokena pozivom
89 HttpClient httpClient = new HttpClient();
90 string response = await httpClient.GetStringAsync(new Uri("https://foursquare.com/oauth2/access_token?client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=authorization_code&redirect_uri=" + redirectUri + "&code=" + code));
91 JsonObject value = JsonValue.Parse(response).GetObject();
92 OAuthToken = value.GetNamedString("access_token");
93 }
94 }
95}