Skip to content

Commit 538e2e3

Browse files
author
werwolf2303
committed
Token request via login5
Mercury responds with 403 instead of the token. Fixed via login5
1 parent e57d4ba commit 538e2e3

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/main/java/com/spotifyxp/Initiator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ static void checkTrustStore() {
133133
throw new RuntimeException(ex);
134134
}
135135
}
136-
} catch (IOException e) {
137-
throw new RuntimeException(e);
136+
} catch (IOException ignored) {
138137
}
139138
}
140139

src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/core/TokenProvider.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818

1919
import com.google.gson.JsonArray;
2020
import com.google.gson.JsonObject;
21+
import com.google.protobuf.ByteString;
22+
import com.spotifyxp.deps.com.spotify.login5v3.Credentials;
23+
import com.spotifyxp.deps.com.spotify.login5v3.Login5;
2124
import com.spotifyxp.deps.xyz.gianlu.librespot.common.Utils;
22-
import com.spotifyxp.deps.xyz.gianlu.librespot.json.GenericJson;
2325
import com.spotifyxp.deps.xyz.gianlu.librespot.mercury.MercuryClient;
24-
import com.spotifyxp.deps.xyz.gianlu.librespot.mercury.MercuryRequests;
2526
import com.spotifyxp.logging.ConsoleLoggingModules;
2627
import org.jetbrains.annotations.NotNull;
2728
import org.jetbrains.annotations.Nullable;
28-
2929
import java.io.IOException;
30+
import java.security.NoSuchAlgorithmException;
3031
import java.util.ArrayList;
3132
import java.util.Arrays;
3233
import java.util.List;
@@ -64,8 +65,38 @@ public synchronized StoredToken getToken(@NotNull String... scopes) throws IOExc
6465
}
6566

6667
ConsoleLoggingModules.debug("Token expired or not suitable, requesting again. {scopes: {}, oldToken: {}}", Arrays.asList(scopes), token);
67-
GenericJson resp = session.mercury().sendSync(MercuryRequests.requestToken(session.deviceId(), String.join(",", scopes)));
68-
token = new StoredToken(resp.obj);
68+
/*GenericJson resp = session.mercury().sendSync(MercuryRequests.requestToken(session.deviceId(), String.join(",", scopes)));
69+
token = new StoredToken(resp.obj);*/
70+
71+
//ToDo: Change MercuryException to NoSuchAlgorithmException when an official fix is available
72+
try {
73+
Login5Api api = new Login5Api(session);
74+
Login5.LoginResponse resp = api.login5(
75+
Login5.LoginRequest.newBuilder()
76+
.setStoredCredential(Credentials.StoredCredential.newBuilder()
77+
.setUsername(session.username())
78+
.setData(ByteString.copyFrom(session.apWelcome().getReusableAuthCredentials().toByteArray()))
79+
.build())
80+
.build()
81+
);
82+
if (!resp.hasOk()) throw new IOException("Login5 returned an error: " + resp.getError());
83+
Login5.LoginOk okResponse = resp.getOk();
84+
85+
JsonObject tokenBuilder = new JsonObject();
86+
tokenBuilder.addProperty("accessToken", okResponse.getAccessToken());
87+
tokenBuilder.addProperty("expiresIn", okResponse.getAccessTokenExpiresIn());
88+
tokenBuilder.addProperty("tokenType", "Bearer");
89+
90+
JsonArray scopesArray = new JsonArray();
91+
for (String scope : scopes)
92+
scopesArray.add(scope);
93+
94+
tokenBuilder.add("scope", scopesArray);
95+
96+
token = new StoredToken(tokenBuilder);
97+
}catch (NoSuchAlgorithmException e) {
98+
throw new IOException(e);
99+
}
69100

70101
ConsoleLoggingModules.debug("Updated token successfully! {scopes: {}, newToken: {}}", Arrays.asList(scopes), token);
71102
tokens.add(token);

0 commit comments

Comments
 (0)