Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fleet API support #36

Merged
merged 8 commits into from
Oct 18, 2023
Merged

Fleet API support #36

merged 8 commits into from
Oct 18, 2023

Conversation

tomhollander
Copy link
Owner

Updated the library to support the Fleet API.
Removed the code that supported the pure API (browserless) authentication since it doesn't work anymore.
Updated WPF sample
Added new ASP.NET Core web sample
Updated README.

Note, I've tested this with my own apps registered for the Tesla Fleet API. While I can successfully obtain tokens, I'm yet to figure out the partner registration process so I haven't proven the tokens can be used. I'll get to that soon but I was keen to get others to try the API in the meantime.

@tomhollander
Copy link
Owner Author

@brianflex @briangru @ramonsmits I'd appreciate your review if you have the time.

Copy link
Contributor

@ramonsmits ramonsmits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only quickly reviewed the lib changes. I'll review but more importantly test from the EU (Netherlands).

public static string EnergyDeviceData = "energy_device_data";
public static string EnergyCommands = "energy_cmds";

public static string GetScopeString(params string[] scopes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a public API I guess it would be better that it would be based on an IEnumerable<string> but that is my personal preference.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that still work with params?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't but it would allow to just pass for example a HashSet or List.

public static string GetScopeString(params string[] scopes)
{
var sb = new StringBuilder();
sb.Append("openid offline_access ");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these also scopes? Why not add this as public static strings too?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my reading of the docs, these are always needed, so I thought it was best if users don't need to specify them every time.

@@ -33,15 +33,29 @@ public class TeslaAuthHelper
{
const string TESLA_CLIENT_ID = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384";
const string TESLA_CLIENT_SECRET = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3";
const string TESLA_REDIRECT_URI = "https://auth.tesla.com/void/callback\"";
const string TESLA_SCOPES = "openid email offline_access";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also scopes that should be used from Scopes?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the legacy Owner API scopes.

string code = HttpUtility.ParseQueryString(location.Query).Get("code");
return code;
}


async Task<Tokens> ExchangeCodeForBearerTokenAsync(string code, HttpClient client, CancellationToken cancellationToken)
{
var body = new JObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use System.Text.Json.Nodes ? Could potentially remove the dependency on Newtonsoft.Json

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks will do

};
{"redirect_uri", redirectUri},
{ "scope", scopes },
{ "audience", "https://fleet-api.prd.na.vn.cloud.tesla.com" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but isn't this url based on region? See https://developer.tesla.com/docs/fleet-api#overview

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch this was on my Todo list and I missed it. Although it would be good to test if this actually matters.

@tomhollander
Copy link
Owner Author

Thanks @ramonsmits - I addressed most of your feedback, and also improved comments and added a new console sample.
I didn't move away from Newtonsoft.Json as that would require bumping the .NET Standard version to 2.1 which would change the set of supported platforms. Not sure if this matters or not but I didn't want to do it without some thought.
LMK what you think of the changes.

library/TeslaAuthHelper.cs Outdated Show resolved Hide resolved
library/TeslaAuthHelper.cs Outdated Show resolved Hide resolved
tomhollander and others added 3 commits October 17, 2023 18:56
Co-authored-by: Ramon Smits <ramon.smits@gmail.com>
Co-authored-by: Ramon Smits <ramon.smits@gmail.com>
@tomhollander
Copy link
Owner Author

@ramonsmits Thanks for the fast reviews. Have you been able to test it yet?

@ramonsmits
Copy link
Contributor

ramonsmits commented Oct 18, 2023 via email

@tomhollander tomhollander merged commit b78a62a into master Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants