【问题标题】:Spotify pass Variables + redirect URLSpotify 传递变量 + 重定向 URL
【发布时间】: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.phpadd.php

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

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

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

【问题讨论】:

    标签: java php spotify


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      @cKarsten 解决方案在我看来是开销,因为 $_POST['track'] 的值只需要一次,并且在重定向到的页面中。此外,数据被不必要地存储。

      我宁愿修改index.php,将$_POST['track'] 的值作为get-paramater 传递给app.php。分别在 app.php 中的$_POST['track'] 必须改为$_GET['track']

      我演示一下。以下是不安全的,但显示了我会这样做的方式。 修改后的代码在上一行用“// ADDED:”标记。

      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
      ));
      
      // ADDED: if $authorizeUrl contains at least one get-paramater use "&". Otherwise "?". In this sample I am using "&".
      $authorizeUrl .= "&track=".$_POST['track'];
      
      header('Location: ' . $authorizeUrl);
      die();   
      // Start using the API!
      

      别忘了更新add.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        • 2015-03-20
        • 2017-12-19
        • 2013-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多