public static (string, int) GetPortalToken(string user, string pw) { using (var myWebClient = new WebClientNoRedir() { Proxy = GetProxy() }) { var WS_Data = new NameValueCollection { { "client_id", "id" }, //you need to replace it with actual values { "username", user }, { "password", pw }, { "redirect_uri", "https%3A%2F%2Fwww.bmw-connecteddrive.com%2Fapp%2Fdefault%2Fstatic%2Fexternal-dispatch.html" }, { "state", "state" }, //you need to replace it with actual values { "response_type", "token" }, { "scope", "authenticate_user fupo" } }; myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); string response = "", expires = ""; string bodyRaw = "&client_id=id&redirect_uri=https%3A%2F%2Fwww.bmw-connecteddrive.com%2Fapp%2Fdefault%2Fstatic%2Fexternal-dispatch.html&response_type=token&scope=authenticate_user%20fupo&state=state&locale=DE-de&username=email&password=pass"; try { // response = myWebClient.UploadString("https://customer.bmwgroup.com/gcdm/oauth/authenticate", WS_Data.ToQueryString(isEscaped: true)); response = myWebClient.UploadString("https://customer.bmwgroup.com/gcdm/oauth/authenticate", bodyRaw); var headerCollection = myWebClient.ResponseHeaders; var tokenInside = headerCollection["Location"]; response = System.Web.HttpUtility.ParseQueryString(tokenInside)["access_token"]; expires = System.Web.HttpUtility.ParseQueryString(tokenInside)["expires_in"]; } catch (System.Exception ex) { Trace.Write("TokenRenew"); Trace.WriteLine(ex.Message); } return(response, int.Parse(expires)); } }
public static (string, DateTime) GetAPIToken(string user, string pw) { using (var myWebClient = new WebClientNoRedir() { Proxy = GetProxy() }) { //string key = "nQv6CqtxJuXWP74xf3CJwUEP"; //string secret = "1zDHx6un4cDjybLENN3kyfumX2kEYigWPcQpdvDRpIBk7rOJ"; //string basic = ($"{key}:{secret}").ToBase64String(); var WS_Data = new NameValueCollection { { "username", user }, { "password", pw }, { "scope", "authenticate_user remote_services vehicle_data" }, { "client_id", "dbf0a542-ebd1-4ff0-a9a7-55172fbfce35" }, { "response_type", "token" }, { "redirect_uri", "https://www.bmw-connecteddrive.com/app/static/external-dispatch.html" } }; // myWebClient.Headers.Add("Authorization", "Basic " + basic); myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); string response = ""; try { response = myWebClient.UploadString($"{AuthServer}", WS_Data.ToQueryString(isEscaped: false)); } catch (System.Exception ex) { Trace.Write("TokenRenew"); Trace.WriteLine(ex.Message); } string tokenInside = new Uri(myWebClient.ResponseHeaders["Location"]).Fragment.Remove(0, 1); response = System.Web.HttpUtility.ParseQueryString(tokenInside)["access_token"]; string expires = System.Web.HttpUtility.ParseQueryString(tokenInside)["expires_in"]; return(response, DateTime.Now.AddSeconds(int.Parse(expires) - 10)); } }