public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); GoogleKey googleKey = new GoogleKey { type = config["type"], project_id = config["project_id"], private_key_id = config["private_key_id"], private_key = config["private_key"], client_email = config["client_email"], client_id = config["client_id"], auth_uri = config["auth_uri"], token_uri = config["token_uri"], auth_provider_x509_cert_url = config["auth_provider_x509_cert_url"], client_x509_cert_url = config["client_x509_cert_url"] }; var googleKeyJson = JsonConvert.SerializeObject(googleKey); log.LogError(googleKeyJson); string token = GetAccessTokenFromJSONKey(googleKeyJson, "https://www.googleapis.com/auth/analytics.readonly"); return(token != null ? (ActionResult) new OkObjectResult(token) : new BadRequestObjectResult("Token not retrieve")); }
private void SetID(string name) { string googleKey = new GoogleKey().googleKey; ///Quota: 100 for one search - now I have 10000/day so 100/day string query = $"https://www.googleapis.com/youtube/v3/search/?part=snippet%20&maxResults=1&q={name}&key={googleKey}"; string json = ""; try { this.VideoID = "Error" + this.top; WebRequest request = WebRequest.Create(query); request.Credentials = CredentialCache.DefaultCredentials; using (WebResponse response = request.GetResponse()) using (Stream dataStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(dataStream)) { json = reader.ReadToEnd(); reader.Close(); response.Close(); } } catch (WebException e) { //Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++"); //Console.WriteLine(e.Message); //Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++"); this.VideoID = "FirstError" + this.top; Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); Log.Error("First error in Yuotube: " + e.Message); Log.Error(e.StackTrace); //Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++"); } string pattern = "[\"]{1}videoId[\"]{1}[:]{1}[\\s]{1}[\"]{1}([^\"]+)[\"]{1}"; var reg = new Regex(pattern); //string ID = "Error"+this.top_; //this.VideoID = "Error"+this.top_; if (reg.IsMatch(json)) { string ID = reg.Matches(json).Select(s => s.Groups[1].Value).ToArray()[0]; this.VideoID = ID; //Log.Information("I am getting video id "+ID); // Console.WriteLine("--------------F R O M A P I-----------------"); //Console.WriteLine(name); //Console.WriteLine(ID); } }
public static async Task <bool> SendNotification() { try { string message1Json = GetJson("Message1.json"); string keyJson = GetJson("key.json"); GoogleKey googleKey = JsonConvert.DeserializeObject <GoogleKey>(keyJson); //var httpContent = JsonConvert.SerializeObject(fcmBody); var client = new HttpClient(); var authorization = string.Format("key={0}", googleKey.private_key); client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorization); var stringContent = new StringContent(message1Json); stringContent.Headers.Add(@"Content-Length", "0"); stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); string uri = "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send"; var response = await client.PostAsync(uri, stringContent).ConfigureAwait(false); var result = response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(true); } else { return(false); } } catch (TaskCanceledException ex) { return(false); } catch (Exception ex) { return(false); } }
public static async Task <IconDto> GetSpotifyIconDto(string spotifyFrame) { var spotIcon = new IconDto(spotifyFrame, "", "SPOTIFY"); spotIcon.source = "https://developer.spotify.com/assets/branding-guidelines/[email protected]"; spotIcon.title = ""; using (var webClient = new WebClient()) { var keys = new GoogleKey(); var spotifyClient = keys.spotifyClient; var spotifySecret = keys.spotifySecret; var urlRegex = new Regex(@"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$"); if (urlRegex.IsMatch(spotifyFrame)) { try { string urlValue = urlRegex.Match(spotifyFrame).Value; var spotProp = urlValue.Split('/').ToList(); var spotifyId = spotProp.LastOrDefault(); var spotifyType = spotProp[spotProp.Count - 2]; var postparams = new NameValueCollection(); postparams.Add("grant_type", "client_credentials"); var authHeader = Convert.ToBase64String(Encoding.Default.GetBytes($"{spotifyClient}:{spotifySecret}")); webClient.Headers.Add(HttpRequestHeader.Authorization, "Basic " + authHeader); var tokenResponse = webClient.UploadValues("https://accounts.spotify.com/api/token", postparams); var textJson = Encoding.UTF8.GetString(tokenResponse); var tokenObject = JsonConvert.DeserializeObject <GoogleKey>(textJson); string token = tokenObject.access_token; var spotify = new SpotifyClient(token); switch (spotifyType.ToLower()) { case "artist": var artist = await spotify.Artists.Get(spotifyId); spotIcon.source = artist.Images.FirstOrDefault().Url; spotIcon.title = artist.Name; break; case "album": var album = await spotify.Albums.Get(spotifyId); spotIcon.source = album.Images.FirstOrDefault().Url; spotIcon.title = album.Name; break; case "playlist": var playlist = await spotify.Playlists.Get(spotifyId); spotIcon.source = playlist.Images.FirstOrDefault().Url; spotIcon.title = playlist.Name; break; case "track": var track = await spotify.Tracks.Get(spotifyId); spotIcon.source = track.Album.Images.FirstOrDefault().Url; spotIcon.title = track.Name; break; } } catch (Exception ex) { Log.Error($"Error in adding spotify: {ex.Message}"); Log.Error(ex.StackTrace); return(spotIcon); } } } return(spotIcon); }