public async Task<bool> Login() { var url = "https://github.com/login/oauth/authorize?client_id=" + Uri.EscapeDataString(ClientId) + "&redirect_uri=" + Uri.EscapeDataString(RedirectUri) + "&scope=repo&display=popup&response_type=token"; var startUri = new Uri(url); var endUri = new Uri(RedirectUri); var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startUri, endUri); string code = ""; switch (webAuthenticationResult.ResponseStatus) { case WebAuthenticationStatus.Success: var decoder = new WwwFormUrlDecoder(webAuthenticationResult.ResponseData.Replace(RedirectUri + "/?", "")); code = decoder.First(x => x.Name == "code").Value; break; case WebAuthenticationStatus.ErrorHttp: return false; break; default: return false; break; } var c = new HttpClient(); var data = new Dictionary<string, string> { {"client_id", ClientId}, {"client_secret", ClientSecret}, {"code", code} }; var content = new FormUrlEncodedContent(data); var request = await c.PostAsync(Accesstokenuri, content); var result = await request.Content.ReadAsStringAsync(); _accessToken = new WwwFormUrlDecoder(result).GetFirstValueByName("access_token"); return true; }
public static async Task<bool> authenticateUser() { String WordpressURL = "https://public-api.wordpress.com/oauth2/authorize?client_id=428&redirect_uri=http://127.0.0.1&response_type=code"; System.Uri StartUri = new Uri(WordpressURL, UriKind.RelativeOrAbsolute); System.Uri EndUri = new Uri("http://127.0.0.1", UriKind.RelativeOrAbsolute); WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, StartUri, EndUri); if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) { WwwFormUrlDecoder decoder = new WwwFormUrlDecoder(WebAuthenticationResult.ResponseData.ToString()); string authtoken = decoder.First().Value; string post_string = FormatPostRequest(authtoken); var content = new StringContent(post_string); var client = new System.Net.Http.HttpClient(); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); var responseTask = client.PostAsync("https://public-api.wordpress.com/oauth2/token", content); var response = await responseTask; string jsonresponse = await response.Content.ReadAsStringAsync(); user = JsonConvert.DeserializeObject<AuthenticatedUser>(jsonresponse); ApplicationData.Current.LocalSettings.Values["AuthUser"] = Authenticator.user.access_token; postStatus(user.access_token); displayname = await userInfo(user.access_token); Authenticator.displayname = displayname; return true; } else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.UserCancel) { return false; } else { return false; } }