gpt4 book ai didi

java - Spotify 传递变量 + 重定向 URL

转载 作者:行者123 更新时间:2023-12-02 03:52:52 26 4
gpt4 key购买 nike

对于我正在编程的机器人,我遇到了一个问题:

概要及功能

当用户(在聊天应用程序中)写入 !add spotify:track:someHash 时,我正在调用 java 函数,该函数随后调用 PHP -脚本。

仅供引用:我正在使用 this git-project 。我关注了documentation对于授权内容,当我将它们硬编码到源代码中时,我可以添加我的轨道。

让我向您展示重要的片段:

if (spotifyTrack.startsWith("spotify")) {

// this might be important, maybe need to change this?
URL url = new URL("http://www.example.com/spot/index.php");

URLConnection con = url.openConnection();
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
ps.print("track=" + spotifyTrack);
con.getInputStream();
ps.close();
sendMessage.setText("Song added");
}

可以说,我的变量 spotifyTrack 包含如下内容:spotify:track:06faVvKZVHivuKgL8NUMQr

所以,我正在调用我的index.php

require 'vendor/autoload.php';

// I'm calling another script here, add.php, you see why down below
$session = new SpotifyWebAPI\Session('My ID','My Secret','http://www.example.com./spot/add.php');
$api = new SpotifyWebAPI\SpotifyWebAPI();

$scopes = array(
'playlist-read-private',
'user-read-private',
'playlist-modify-public'
);

$authorizeUrl = $session->getAuthorizeUrl(array(
'scope' => $scopes
));

header('Location: ' . $authorizeUrl);
die();
// Start using the API!

这是第二个脚本:add.php

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session('My Id', 'MY Secret','http://www.example.com/spot/add.php');
$api = new SpotifyWebAPI\SpotifyWebAPI();

$scopes = array(
'playlist-read-private',
'user-read-private',
'playlist-modify-public'
);

$authorizeUrl = $session->getAuthorizeUrl(array(
'scope' => $scopes
));

// Request a access token using the code from Spotify
$session->requestAccessToken($_GET['code']);
$accessToken = $session->getAccessToken();

// Set the access token on the API wrapper
$api->setAccessToken($accessToken);

$track = explode(":", $_POST['track']);

// Start using the API!
$api->addUserPlaylistTracks('my user', 'my playlist id', array(
$track[2],
));

正如你所知,有些东西是多余的,而且不是最好的方法。然而,我被困住了,因为我不知道下一步该做什么。您必须指定重定向 URI,因此我添加了 index.phpadd.php

问题:如果我从 java 程序调用index.php 并将index.php 中的URL 更改为index.php,它将以无限重定向循环结束。但是,调用脚本 add.php 将导致丢失 POST-data

根据我的理解:我必须调用index.php,需要重定向来获取代码参数。 (猜测与 OAuth 相关?)

我的问题:如何在重定向(使用 header())后保留 POST 数据而不丢失我的请求?

最佳答案

如果您需要将数据转发到另一个页面,您可以使用 session :

session_start();
$_SESSION['post_data'] = $_POST;

关于java - Spotify 传递变量 + 重定向 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35735631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com