【发布时间】:2016-03-01 23:24:13
【问题描述】:
对于我正在编程的机器人,我遇到了问题:
总结与功能
当用户(在聊天应用程序中)编写 !add spotify:track:someHash 时,我正在调用 java 函数,然后该函数调用 PHP 脚本。
仅供参考:我正在使用this git-project。我按照documentation 获取授权信息,当我在源代码中硬编码它们时,我可以添加我的曲目。
让我告诉你重要的sn-ps:
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.php 和 add.php。
问题:如果我从我的 java 程序调用 index.php 并将我的 index.php 中的 URL 更改为 index.php,它将以无限重定向循环结束。
但是,调用脚本 add.php 会导致POST-data 丢失。
据我了解:我必须调用 index.php,需要重定向才能获取代码参数。 (猜测 OAuth 相关?)
我的问题:如何在重定向后保留 POST 数据(使用header())而不丢失我的请求?
【问题讨论】: