public async Task <ApiResponse <Achievement> > GetAchievementsAsync(XboxUserAuthInfo info, string continuationToken = null) { var url = string.Format(stub, info.Xuid, mccTitleId, continuationToken); var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Authorization = new AuthenticationHeaderValue("XBL3.0", info.AuthToken); request.Headers.Add("x-xbl-contract-version", "4"); var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { try { var content = await response.Content.ReadAsStringAsync(); return(JsonSerializer.Deserialize <ApiResponse <Achievement> >(content, jsonOptions)); } catch (Exception e) { throw new Exception("Error deserializing achievments from Xbox API", e); } } else { throw new Exception("Error getting achievements from Xbox API: [" + response.StatusCode + "] " + response.ReasonPhrase); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { if (req.Headers.TryGetValue(AuthHeader, out var authHeaders) == false) { return(new UnauthorizedResult()); } XboxUserAuthInfo authInfo = null; foreach (var header in authHeaders) { if (header.StartsWith(AuthHeaderScheme) == false) { continue; } var token = header.Substring(AuthHeaderScheme.Length); try { var json = Crypto.Decrypt(token); authInfo = JsonSerializer.Deserialize <XboxUserAuthInfo>(json); } catch (Exception e) { log.LogError(e, "Failure parsing Authorization header"); return(new UnauthorizedResult()); } } if (authInfo == null) { return(new UnauthorizedResult()); } var client = new HttpClient(); var xbox = new XboxApiClient(client); var achievements = new List <Achievement>(); string continuationToken = null; do { var resp = await xbox.GetAchievementsAsync(authInfo, continuationToken); achievements.AddRange(resp.Achievements); continuationToken = resp.PagingInfo.ContinuationToken; } while (continuationToken != null); var vms = achievements.Select(a => new AchievementViewModel(a)); return(new OkObjectResult(vms)); }