@@ -16,7 +16,11 @@ public class ZwiftPowerService
16
16
private readonly HttpClient _httpClient ;
17
17
private readonly JsonSerializerOptions _options ;
18
18
private readonly IConfiguration _config ;
19
-
19
+
20
+ private string _accessToken ;
21
+ //private string _refreshToken;
22
+ //private int _refreshExpiresIn;
23
+
20
24
public HttpClient Client { get => _httpClient ; }
21
25
22
26
public ZwiftPowerService ( HttpClient httpClient , IConfiguration configuration )
@@ -111,13 +115,27 @@ public async Task Login()
111
115
{
112
116
{ "username" , _config [ "Zwiftpower:Username" ] } ,
113
117
{ "password" , _config [ "Zwiftpower:Password" ] } ,
114
- { "redirect " , "./index.php " } ,
115
- { "login " , "Login " }
118
+ { "client_id " , "Zwift Game Client " } ,
119
+ { "grant_type " , "password " }
116
120
} ) ;
117
121
118
- using var indexPage = await _httpClient . PostAsync ( "/ucp.php?mode=login" , content ) ;
122
+ var httpClient = new HttpClient ( ) ;
123
+
124
+ using var response = await httpClient . PostAsync ( "https://secure.zwift.com/auth/realms/zwift/protocol/openid-connect/token" , content ) ;
125
+
126
+ var loginResponse = await response . Content . ReadFromJsonAsync < LoginResponse > ( ) ;
127
+
128
+ _accessToken = loginResponse . access_token ;
129
+
130
+ _httpClient . DefaultRequestHeaders . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Bearer" , this . _accessToken ) ;
131
+
132
+ await _httpClient . GetAsync ( "/ucp.php?mode=login&login=external&oauth_service=oauthzpsso" ) ;
119
133
}
120
134
135
+ public string BearerToken ( ) { return _accessToken ; }
136
+
137
+ public record LoginResponse ( string access_token , string refresh_token , int expires_in , int refresh_expires_in , string session_state ) ;
138
+
121
139
internal class Data < T >
122
140
{
123
141
#pragma warning disable 0649 // used by JSON deserialization
0 commit comments