Skip to content
This repository was archived by the owner on Aug 3, 2020. It is now read-only.

Commit 44ddb27

Browse files
authored
Merge pull request #28 from BMXsanko/Add-getAuthToken-functionality
Add get auth token functionality
2 parents 3e0a0e9 + 326db03 commit 44ddb27

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ android {
4545
dependencies {
4646
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4747
implementation(name:'spotify-app-remote-release-0.6.1', ext:'aar')
48+
implementation(name:'spotify-auth-release-1.1.0', ext:'aar')
4849
implementation "com.google.code.gson:gson:2.8.2"
4950
}
51+
39.3 KB
Binary file not shown.

android/src/main/kotlin/io/feesie/spotify/spotifyplayback/SpotifyPlaybackPlugin.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.feesie.spotify.playback
22

3+
import android.content.Intent
4+
import android.content.IntentFilter
5+
import android.net.ConnectivityManager
6+
import android.os.Parcel
37
import android.util.Log
48
import com.spotify.android.appremote.api.ConnectionParams
59
import com.spotify.android.appremote.api.Connector
@@ -20,6 +24,13 @@ import io.flutter.plugin.common.PluginRegistry
2024
import io.flutter.plugin.common.PluginRegistry.Registrar
2125
import kotlin.concurrent.fixedRateTimer
2226

27+
import com.spotify.sdk.android.authentication.AuthenticationClient;
28+
import com.spotify.sdk.android.authentication.AuthenticationHandler
29+
import com.spotify.sdk.android.authentication.AuthenticationRequest;
30+
import com.spotify.sdk.android.authentication.AuthenticationResponse;
31+
32+
33+
2334
class SpotifyPlaybackPlugin(private var registrar: PluginRegistry.Registrar) : MethodCallHandler,
2435
StreamHandler {
2536

@@ -78,9 +89,36 @@ class SpotifyPlaybackPlugin(private var registrar: PluginRegistry.Registrar) : M
7889
uri = call.argument("uri"), result = result, quality = call.argument("quality"),
7990
size = call.argument("size")
8091
)
92+
call.method == "getAuthToken" -> getAuthToken(
93+
clientId = call.argument("clientId"),
94+
redirectUrl = call.argument("redirectUrl"),
95+
result = result)
8196
}
8297
}
8398

99+
//THIS SHOULD ONLY BE CALLED ONCE
100+
private fun getAuthToken(clientId: String?,
101+
redirectUrl: String?,
102+
result: Result){
103+
104+
try {
105+
AuthenticationClient.openLoginActivity(
106+
registrar.activity(), 1337,
107+
AuthenticationRequest.Builder(clientId,AuthenticationResponse.Type.TOKEN,redirectUrl)
108+
.setScopes(arrayOf("user-modify-playback-state")).build())
109+
}catch (err:Throwable){
110+
Log.v("getAuthTOkenError",err.message)
111+
}
112+
registrar.addActivityResultListener { requestCode, resultCode, intent ->
113+
if (requestCode == 1337){
114+
result.success(AuthenticationClient.getResponse(resultCode,intent).accessToken)
115+
}
116+
true
117+
}
118+
}
119+
120+
121+
84122
/**
85123
* The onlisten for the eventChannel is still in active development
86124
* This eventChannel onListen is used to inform the application about changes to the
@@ -178,6 +216,7 @@ class SpotifyPlaybackPlugin(private var registrar: PluginRegistry.Registrar) : M
178216
}
179217
}
180218

219+
181220
/**
182221
* Get if the spotify sdk is connected, if so return true
183222
*/
39.3 KB
Binary file not shown.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
configurations.maybeCreate("default")
2-
artifacts.add("default", file('spotify-app-remote-release-0.6.1.aar'))
2+
artifacts.add("default", file('spotify-app-remote-release-0.6.1.aar'))
3+
artifacts.add("default", file('spotify-auth-release-1.1.0.aar'))
4+

example/lib/main.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ class _MyAppState extends State<MyApp> {
231231
}
232232
}
233233

234+
Future<void> getAuthToken() async {
235+
try {
236+
SpotifyPlayback.getAuthToken(clientId: Credentials.clientId,redirectUrl: Credentials.redirectUrl).then((authToken){
237+
print(authToken);
238+
});
239+
240+
} on PlatformException {
241+
print('Failed to play.');
242+
}
243+
}
244+
234245
@override
235246
Widget build(BuildContext context) {
236247
return MaterialApp(
@@ -302,6 +313,12 @@ class _MyAppState extends State<MyApp> {
302313
"https://i.scdn.co/image/3269971d34d3f17f16efc2dfa95e302cc961a36c"),
303314
),
304315
child: Text("Get image")),
316+
317+
RaisedButton(
318+
onPressed: () => getAuthToken(),
319+
child: Text("Auth token"),
320+
),
321+
305322
(image != null)
306323
? Image.memory(
307324
image,

lib/spotify_playback.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ class SpotifyPlayback {
129129
static String imageLinkToURi(String imageLink) {
130130
return "spotify:image:" + imageLink.replaceRange(0, imageLink.lastIndexOf("/") + 1, "");
131131
}
132+
133+
/// This method is used to get the authToken
134+
static Future<String> getAuthToken(
135+
{@required String clientId, @required String redirectUrl}) async {
136+
final String token = await _channel.invokeMethod(
137+
"getAuthToken", {"clientId": clientId, "redirectUrl": redirectUrl});
138+
return token;
139+
}
140+
141+
132142
}
133143

134144
enum ImageDimension{

0 commit comments

Comments
 (0)