jjzjj

php - Soundcloud API、PHP 和 OAuth

coder 2024-07-01 原文

我正在构建一个站点,我需要从我的 soundcloud 帐户查询我的最后两首轨道并将它们显示在我的页面上。 我已经阅读了 Soundcloud API 文档,但它似乎晦涩难懂而且遥不可及。 我已经安装了用于使用 API 和 Oauth 的 PHP 库,并设置了我的 SoundCloud 应用程序以获取我的消费者 key ,但我无法启动 OAuth session 。

我正在使用这个 library .

我需要从我的 Soundcloud 帐户中检索最后 2 首轨道。在我需要库中的文件(soundcloud.php 和 oauth.php)后,我需要设置四个参数:$consumer_key、$consumer_secret、$callback_url、$tmp_path。

我已经有了我的 key 和我的可写缓存文件夹。我不知道我的回调 url 是什么。此外,我必须说我找不到任何可用的示例代码,所以我什至无法开始编写任何东西。这么堵!

有没有什么方法可以在不调用另一个窗口的情况下自动执行 OAuth 流程,以便在我的 PHP 脚本中请求我的 OAuth token ?

我想知道您是否可以给我一些执行此操作的示例代码。 那太好了!!

最佳答案

这可能对您有用。登录后,它会将我最喜欢的轨道嵌入到页面中。 您可以更改 $favs 以加载您自己的歌曲而不是您的收藏夹。

另请注意,我的 config.php 包括我的 consumer_key、c​​onsumer_secret 和我的 callback_url。

$callback_url = 'http://localhost/soundcloud';

你希望它等于你的 php 脚本所在的位置。

<?php
  require_once ('php-soundcloud/mptre-php-soundcloud-644bb0e/oauth.php');
  require_once ('php-soundcloud/mptre-php-soundcloud-644bb0e/soundcloud.php');
  require_once ('config.php');

session_start();

// Clear the session i.e delete all stored tokens.
if (isset($_GET['logout'])) {
    session_destroy();
}

// Variables used for verifying the status of the "OAuth dance".
$oauth_token = (isset($_GET['oauth_verifier']))
    ? $_GET['oauth_verifier']
    : ((isset($_SESSION['oauth_access_token'])) ? $_SESSION['oauth_access_token'] : NULL);
$oauth_request_token = (isset($_SESSION['oauth_request_token']))
    ? $_SESSION['oauth_request_token']
    : NULL;
$oauth_request_token_secret = (isset($_SESSION['oauth_request_token_secret']))
    ? $_SESSION['oauth_request_token_secret']
    : NULL;

if (isset($oauth_token) && isset($oauth_request_token) && isset($oauth_request_token_secret)) {
    // Retreive access tokens if missing.
    if (!isset($_SESSION['oauth_access_token']) && !isset($_SESSION['oauth_access_token_secret'])) {
        $soundcloud = new Soundcloud(
            $consumer_key,
            $consumer_secret,
            $_SESSION['oauth_request_token'],
            $_SESSION['oauth_request_token_secret']
        );
        $token = $soundcloud->get_access_token($oauth_token);
        $_SESSION['oauth_access_token'] = $token['oauth_token'];
        $_SESSION['oauth_access_token_secret'] = $token['oauth_token_secret'];
    }

    // Construct a fully authicated connection with SoundCloud.
    $soundcloud = new Soundcloud(
        $consumer_key,
        $consumer_secret,
        $_SESSION['oauth_access_token'],
        $_SESSION['oauth_access_token_secret']
    );

    // Get basic info about the authicated visitor.
    $me = $soundcloud->request('me');
    $me = new SimpleXMLElement($me);
    $me = get_object_vars($me);

    // Get some embedding code for favs
    $favs = $soundcloud->request('http://api.soundcloud.com/users/'.$me['id'].'/favorites/');
    $favs = new SimpleXMLElement($favs);

} else {
    // This is the first step in the "OAuth dance" where we ask the visitior to authicate himself.
    $soundcloud = new Soundcloud($consumer_key, $consumer_secret);
    $token = $soundcloud->get_request_token($callback_url);

    $_SESSION['oauth_request_token'] = $token['oauth_token'];
    $_SESSION['oauth_request_token_secret'] = $token['oauth_token_secret'];

    $login = $soundcloud->get_authorize_url($token['oauth_token']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>SoundCloud PHP API Wrapper</title>
    <meta name="author" content="Anton Lindqvist" />
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/reset/reset-min.css" />
    <link rel="stylesheet" type="text/css" href="assets/css/style.css" />
</head>
<body>
    <div id="wrapper">
        <div id="content">
            <?php if (isset($me)): ?>
                <a class="logout" href="?logout=true">logout</a>
            <?php endif; ?>
            <div id="header">
                <h1>SoundCloud PHP API Wrapper</h1>
            </div>
            <?php if (isset($login)): ?>
            <h2>What is this?</h2>
            <p>This is a basic demo</p>
            <h2>How to start?</h2>
            <p><a class="button" href="<?php echo $login; ?>">login with your SoundCloud account</a></p>
            <?php elseif (isset($me)): ?>
                <div id="profile">
                  <h2>
                    <a href="<?php echo $me['permalink-url']; ?>"><?php echo $me['permalink']; ?></a>
                  </h2>
                </div>
                <div class="clear"></div>

                <div id="favs">
                <?php
                  if (isset($favs)){

                    foreach($favs->track as $fav){
                        $permalink_url = $fav->{'permalink-url'};
                        $permalink_url = urlencode($permalink_url);

                        $f = simplexml_load_file('http://soundcloud.com/oembed?url='.$permalink_url);
                        echo $f->html;
                    }

                  } else {
                     echo "fail";
                  }
                ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>

另外请注意,我是一个 php 新手,第一次使用这个 api...所以我的能力暂时不会超过这个。其中大部分是从您正在使用的 php 包装器库附带的演示中“借用”的。

希望它有所帮助:)

附言。不确定是否有一种方法可以在不调用另一个窗口的情况下自动化 OAuth 过程。

关于php - Soundcloud API、PHP 和 OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2715305/

有关php - Soundcloud API、PHP 和 OAuth的更多相关文章

  1. ruby-on-rails - Rails 两条腿的 OAuth 提供商? - 2

    我有一个Rails2.3.5应用程序,其中包含我希望保护的API。没有用户-它是一个应用到应用风格的网络服务(更像是亚马逊服务而不是facebook),所以我想使用两条腿的OAuth方法来实现它。我一直在尝试使用oauth-plugin服务器实现作为开始:http://github.com/pelle/oauth-plugin...但它的构建需要三足(网络重定向流)oauth。在我深入研究对其进行更改以支持两条腿之前,我想看看是否有更简单的方法,或者是否有人有更好的方法让Rails应用程序实现成为两条腿的OAuth提供程序。 最佳答案

  2. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  3. ruby - 如何使用 omniauth/oauth 对每秒登录数进行基准测试? ( ruby +rspec) - 2

    我想用一个(自己的)omniauth提供商来衡量每秒可以登录多少次。我需要了解此omniauth/oauth请求的性能如何,以及此身份验证是否具有可扩展性?到目前为止我得到了什么:defperformance_auth(user_count=10)bm=Benchmark.realtimedouser_count.timesdo|n|forkdoclick_on'Logout'omniauth_config_mock(:provider=>"foo",:uid=>n,:email=>"foo#{n}@example.net")visit"/account/auth/foo/"enden

  4. ruby-on-rails - 如何编写 Rails 4 测试以使用 omniauth-google-oauth2 gem 创建 session ? - 2

    我正在尝试为使用omniauth-google-oauth2gem创建session编写测试。我是否需要将env["omniauth.auth"]变量与post:create一起传递?也许当我试图这样做时,我做错了。我得到的错误如下所示...Rake测试错误1)Error:SessionsControllerTest#test_should_get_create:NoMethodError:undefinedmethod`provider'fornil:NilClassapp/models/user.rb:6:in`from_omniauth'app/controllers/sessi

  5. ruby-on-rails - 将 OAuth 与 ActiveResource 一起使用的最简单方法是什么? - 2

    我正在使用一些旧代码并使用ActiveResource进行非常基本的Twitter集成。我想尽可能少地接触应用程序代码,并在仍然使用ActiveResource的同时引入OAuth。不幸的是,我找不到简单的方法来做到这一点。我确实遇到了oauth-active-resourcegem,但它并没有完全记录下来,而且它似乎是为创建完整的API包装器库而设计的。您可以想象,我想避免为这一遗留更改创建整个TwitterActiveResourceAPI包装器。有什么成功案例吗?在我的例子中,离开ActiveResource可能比让它工作更快。我很高兴被证明是错误的!

  6. ruby-on-rails - rails 中的 Omniauth-twitter:OAuth::Unauthorized 401 - 2

    我在使用Twitter进行基本的omniauth身份验证时被封锁了2天。我在简单的omniauth上跟随RyanBates的railscast,但无法通过OAuth::Unauthorized401异常,当我尝试登录时引发。请帮忙!我的代码粘贴在下面:twitterinfo:website:[http://127.0.0.1:3000]callbarckurl:[http://127.0.0.1:3000/auth/twitter/callback]//路线.rbSentimentalist::Application.routes.drawdoresources:dashboard,o

  7. ruby - 带有 OAuth2 : update and delete fail with "Insufficient Permission" error 的 YouTube API v3 - 2

    我正在尝试使用YouTubeAPIv3来更新和删除视频与OAuth2forauthentication通过google-api-client(0.6.4)Rubygem。但是,当我尝试执行这两个操作中的任何一个时,我看到以下错误消息:Google::APIClient::ClientError:InsufficientPermission奇怪的是:使用与update和delete完全相同的身份验证过程,我可以insert(上传)成功,没问题!所以,我不认为这是我的身份验证设置的问题,而是我代码中的其他地方。我的读写scope在所有这些操作中始终相同:https://www.google

  8. ruby-on-rails - Rails 还是 Sinatra? PHP程序员入门学习哪个好? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?

  9. ruby-on-rails - PHP 魔术方法 __call、__get 和 __set 的 Ruby 等价物 - 2

    我很确定Ruby有这些(等同于__call、__get和__set),否则find_by将如何在Rails中工作?也许有人可以举一个简单的例子来说明如何定义与find_by相同的方法?谢谢 最佳答案 简而言之你可以映射__调用带有参数的method_missing调用__设置为方法名称以'='结尾的method_missing调用__获取不带任何参数的method_missing调用__调用PHPclassMethodTest{publicfunction__call($name,$arguments){echo"Callingob

  10. ruby - Lisp - 是否适合网络编程/应用程序(交互式)? ruby 的方式是? php的方式是? - 2

    Lisp是否适合Web编程/应用程序(交互式),就像ruby​​和php一样?需要考虑的事情是:易于使用可部署性难度(尤其是对于编程初学者而言)(编辑)在阅读PaulGraham'sessay之后,我特别提到了CommonLisp.将是我的第一门编程语言。在这方面。这样做合适吗?我听说Clojure的宏功能不如CommonLisp的强大,这就是我尝试学习Clojure的原因。它教授编程并且非常强大。 最佳答案 Lisp是一个语系,而不是单一的语言。为了稍微回答您的问题,是的,存在用于各种Lisp方言的Web框架,例如用于Common

随机推荐