/// <summary> /// Creates a new client to interact with Uber's API /// </summary> /// <param name="clientid"></param> /// <param name="clientsecret"></param> /// <param name="redirect_uri"></param> /// <param name="scope"></param> public UberClient(string clientid, string clientsecret, string redirect_uri, string[] scope) { UberConstants.ClientId = clientid; UberConstants.StoreSecret(UberConstants.ClientSecretUserName, clientsecret); this.redirect_uri = redirect_uri; this.scope = scope; }
/// <summary> /// Oauth2.0 Flow for Uber /// </summary> /// <param name="clientID">ClientID of the Uber app</param> /// <param name="redirect_uri">Redirect URI</param> /// <param name="scope">List of space delimited scopes to request from Uber</param> public static async Task <bool> Oauth2Flow(string clientID, string redirect_uri, string clientsecret, string scope) { if (!String.IsNullOrEmpty(UberConstants.ReadSecret(UberConstants.ClientSecretUserName))) { return(true); } WebAuthenticationResult authCodeResult = await GetAuthorizationCode(clientID, redirect_uri, scope); if (authCodeResult.ResponseStatus == WebAuthenticationStatus.Success) { string responseData = authCodeResult.ResponseData.ToString(); string subResponseData = responseData.Substring(responseData.IndexOf("code")); String[] keyValPairs = subResponseData.Split('='); string authCode = keyValPairs[1]; Dictionary <string, string> pairs = new Dictionary <string, string> { { "client_secret", clientsecret }, { "client_id", clientID }, { "grant_type", "authorization_code" }, { "redirect_uri", redirect_uri }, { "code", authCode } }; HttpFormUrlEncodedContent formContent = new HttpFormUrlEncodedContent(pairs); HttpClient client = new HttpClient(); var httpresponseMessage = await client.PostAsync(new Uri(UberAPI.UberAccessTokenUrl), formContent); string response = await httpresponseMessage.Content.ReadAsStringAsync(); UberAuthModel authResponse = JsonConvert.DeserializeObject <UberAuthModel>(response); UberConstants.StoreSecret(UberConstants.AccessTokenUserName, authResponse.access_token); UberConstants.StoreSecret(UberConstants.RefreshTokenUserName, authResponse.refresh_token); return(true); } return(false); }