private void AuthorizationGiven() { ////ONCE USER GIVES AUTHORIZATION IN FLICKRWEB GET THAT VERIFICATION FOR THE APP //flickr.AuthGetTokenAsync(frob, new Action<FlickrNet.FlickrResult<Auth>>(auth => //{ // if (!auth.HasError) // { // flickr_Auth = auth.Result; // Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.High, new Windows.UI.Core.InvokedHandler((r, a) => // { // butAuthorizationGiven.IsEnabled = false; // }), this, null); // GetLoggedInUserDetails(flickr_Auth.User); // } //})); //3. COPY THE VERIFICATION CODE FROM THE FLICKR PAGE AND USE IT TO GET AN "ACCESS" TOKEN string OAuthVerificationCode = ""; //txtOAuthVerificationCode.Text if (OAuthVerificationCode.Length == 0) { return; } _flickr.OAuthGetAccessTokenAsync(rt, OAuthVerificationCode, new Action <FlickrResult <OAuthAccessToken> >(rat => { if (!rat.HasError) { at = rat.Result; //USE YOUR ACCESS TO START MAKING API CALLS GetLoggedInUserDetails(at.UserId); } })); }
private async void AttemptFlickrLogin() { //apiKey = ctlApiEditor.APIKey; try { _flickr.ApiKey = apiKey.APIKey; _flickr.ApiSecret = apiKey.APISecret; // Acquiring a request token TimeSpan SinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1); Random Rand = new Random(); String FlickrUrl = "https://secure.flickr.com/services/oauth/request_token"; Int32 Nonce = Rand.Next(1000000000); // Compute base signature string and sign it. // This is a common operation that is required for all requests even after the token is obtained. // Parameters need to be sorted in alphabetical order // Keys and values should be URL Encoded. String SigBaseStringParams = "oauth_callback=" + Uri.EscapeDataString(apiKey.APICallbackUrl); SigBaseStringParams += "&" + "oauth_consumer_key=" + apiKey.APIKey; SigBaseStringParams += "&" + "oauth_nonce=" + Nonce.ToString(); SigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1"; SigBaseStringParams += "&" + "oauth_timestamp=" + Math.Round(SinceEpoch.TotalSeconds); SigBaseStringParams += "&" + "oauth_version=1.0"; String SigBaseString = "GET&"; SigBaseString += Uri.EscapeDataString(FlickrUrl) + "&" + Uri.EscapeDataString(SigBaseStringParams); IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(apiKey.APISecret + "&", BinaryStringEncoding.Utf8); MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1"); CryptographicKey MacKey = HmacSha1Provider.CreateKey(KeyMaterial); IBuffer DataToBeSigned = CryptographicBuffer.ConvertStringToBinary(SigBaseString, BinaryStringEncoding.Utf8); IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned); String Signature = CryptographicBuffer.EncodeToBase64String(SignatureBuffer); FlickrUrl += "?" + SigBaseStringParams + "&oauth_signature=" + Uri.EscapeDataString(Signature); string GetResponse = await SendDataAsync(FlickrUrl); //rootPage.NotifyUser("Received Data: " + GetResponse, NotifyType.StatusMessage); if (GetResponse != null) { String oauth_token = null; String oauth_token_secret = null; String[] keyValPairs = GetResponse.Split('&'); for (int i = 0; i < keyValPairs.Length; i++) { String[] splits = keyValPairs[i].Split('='); switch (splits[0]) { case "oauth_token": oauth_token = splits[1]; break; case "oauth_token_secret": oauth_token_secret = splits[1]; break; } } RequestToken = new OAuthRequestToken() { Token = oauth_token, TokenSecret = oauth_token_secret }; if (oauth_token != null) { FlickrUrl = "https://secure.flickr.com/services/oauth/authorize?oauth_token=" + oauth_token + "&perms=read"; System.Uri StartUri = new Uri(FlickrUrl); System.Uri EndUri = new Uri(apiKey.APICallbackUrl.Contains("http") ? apiKey.APICallbackUrl : $"http://{apiKey.APICallbackUrl}"); //rootPage.NotifyUser("Navigating to: " + FlickrUrl, NotifyType.StatusMessage); WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.None, StartUri, EndUri); if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) { OutputToken(WebAuthenticationResult.ResponseData.ToString()); String[] partsa = WebAuthenticationResult.ResponseData.ToString().Split('?'); String[] partsb = partsa[1].Split('&'); var xoauth_token = ""; var xoauth_verifier = ""; for (int i = 0; i < partsb.Length; i++) { String[] partsc = partsb[i].Split('='); switch (partsc[0]) { case "oauth_token": xoauth_token = partsc[1]; break; case "oauth_verifier": xoauth_verifier = partsc[1]; break; } } var rat = await _flickr.OAuthGetAccessTokenAsync(RequestToken, xoauth_verifier); if (!rat.HasError) { AccessToken = rat.Result; var dm = new PassportDataModel(); dm.Token = AccessToken.Token; dm.TokenSecret = AccessToken.TokenSecret; dm.Verifier = xoauth_verifier; dm.PassType = GroupingType; dm.UserId = AccessToken.UserId; dm.UserName = AccessToken.Username; dm.FullName = AccessToken.FullName; dm.ScreenName = AccessToken.ScreenName; dm.APIKeyFKID = apiKey.Id; StorageService.Instance.Storage.Insert(dm); PopulatePassportData(); } } else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp) { OutputToken("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString()); } else { OutputToken("Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString()); } } } } catch //(Exception Error) { //rootPage.NotifyUser(Error.Message, NotifyType.ErrorMessage); } }