-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify-c.ps1
201 lines (167 loc) · 6.47 KB
/
spotify-c.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Spotify API相关信息
# $redirectURI = "http://localhost:9000/callback" # 可以随意设置,但必须在Spotify应用程序设置中配置相同的重定向URI
# Spotify的Access Token请求URL
$tokenEndpoint = "https://accounts.spotify.com/api/token"
# 播放控制url
$apiEndpoint = "https://api.spotify.com/v1/me/player"
$client = Get-Content -Path ".\client" | ConvertFrom-Json
$clientID = $client.clientID
$clientSecret = $client.clientSecret
$redirectURI = $client.redirectURI
function get-Token {
Start-ThreadJob -ScriptBlock {
param (
$redirectUri,
$clientId,
$clientSecret,
$tokenEndpoint
)
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://localhost:9000/")
$listener.Start()
while ($true) {
$context = $listener.GetContext()
$request = $context.Request
# 提取授权码
$queryString = $request.Url.Query
if ($queryString -match "code=([^&]+)") {
$authorizationCode = $matches[1]
# 回复给浏览器
$response = $context.Response
$content = [System.Text.Encoding]::UTF8.GetBytes("Authorization Code Received. You can close this window now.")
$response.OutputStream.Write($content, 0, $content.Length)
$response.Close()
# 停止监听器
$listener.Stop()
# 构建POST请求的数据
$body = @{
grant_type = "authorization_code"
code = $authorizationCode
redirect_uri = $redirectUri
client_id = $clientId
client_secret = $clientSecret
}
# 发送POST请求并获取响应
$response = Invoke-RestMethod -Uri $tokenEndpoint -Method Post -Body $body
$response | Out-File -FilePath ".\tokens.json" -Force
$password = [PSCustomObject]@{
token = $response.access_token
reflushToken = $response.refresh_token
}
$password | ConvertTo-Json | Out-File ".\password.json"
# $securePwd = ConvertTo-SecureString -String $accessToken -AsPlainText -Force
# # 2. 使用ConvertFrom-SecureString将密码加密并保存到文件
# $encryptedPassword = $securePwd | ConvertFrom-SecureString
# $encryptedPassword | Out-File -FilePath ".\pwd.txt"
break # 退出循环
}
else {
# 如果没有找到授权码,返回错误页面或者其他处理
$response = $context.Response
$content = [System.Text.Encoding]::UTF8.GetBytes("Error: Authorization Code not found.")
$response.OutputStream.Write($content, 0, $content.Length)
$response.Close()
}
}
} -ArgumentList $redirectUri, $clientId, $clientSecret, $tokenEndpoint
# 构建授权URL
$authorizeUrl = "https://accounts.spotify.com/authorize?client_id=$clientId&response_type=code&redirect_uri=$redirectUri&scope=user-modify-playback-state user-read-playback-state user-read-currently-playing"
# 打开浏览器并导航到授权URL,用户登录并授权应用程序
Start-Process $authorizeUrl
}
function update-Token {
param (
[string]$RefreshToken,
[string]$ClientId,
[string]$ClientSecret,
[string]$TokenEndpoint = "https://accounts.spotify.com/api/token"
)
$base64Auth = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${ClientId}:${ClientSecret}"))
$headers = @{
'Content-Type' = 'application/x-www-form-urlencoded'
'Authorization' = "Basic $base64Auth"
}
$body = @{
grant_type = 'refresh_token'
refresh_token = $RefreshToken
}
try {
$response = Invoke-RestMethod -Uri $TokenEndpoint -Method Post -Headers $headers -Body $body
# Update your token storage with the new access token and refresh token
# For example, you can save them to a file or update global variables
# $response | Out-File -FilePath ".\tokens.json" -Force
Write-Output $response
# Write-Output "success"
$password = [PSCustomObject]@{
token = $response.access_token
reflushToken = $RefreshToken
}
$password | ConvertTo-Json | Out-File ".\password.json"
return $response.access_token
# $securePwd = ConvertTo-SecureString -String $accessToken -AsPlainText -Force
# # 使用ConvertFrom-SecureString将密码加密并保存到文件
# $encryptedPassword = $securePwd | ConvertFrom-SecureString
# $encryptedPassword | Out-File -FilePath ".\pwd.txt"
}
catch {
Write-Error "Error refreshing access token: $_"
return $null
}
}
function Spotify_C {
param (
[string]$action,
[string]$accessToken
)
# 构建HTTP请求的Header
$headers = @{
Authorization = "Bearer $accessToken"
}
$method = "POST"
switch ($action) {
"p" {
$path = "/previous"
}
"n" {
$path = "/next"
}
"u" {
$path = "/pause"
$method = "PUT"
}
"c" {
$path = "/play"
$method = "PUT"
}
default {
Write-Host "无效的操作: $action"
return
}
}
$url = $apiEndpoint + $path
# Write-Output $url
try {
Invoke-RestMethod -Uri $url -Method $method -Headers $headers -ResponseHeadersVariable Response
return 200
}
catch {
<#Do this if a terminating exception happens#>
# Write-Output "报错"
if ($_.Exception.StatusCode -eq 401) {
# Write-Output "401"
# update-Token -RefreshToken $reflushToken -ClientId $clientID -ClientSecret $clientSecret
return 401
}
# Write-Output $_.Exception
# $memStream = $_.Exception.Response.GetResponseStream()
# $readStream = New-Object System.IO.StreamReader($memStream)
# while ($readStream.Peek() -ne -1) {
# Write-Host $readStream.ReadLine()
# }
# $readStream.Dispose();
return 400
}
}
if (-not (Test-Path -Path ".\password.json")) {
Get-Token
}