Soundcloud (Oauth2) API 获取访问令牌失败 [英] Soundcloud (Oauth2) API getting access token fails

查看:212
本文介绍了Soundcloud (Oauth2) API 获取访问令牌失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据 Soundcloud 登录信息授权我网站上的用户.

I'm trying to authorize users on my site based on a Soundcloud login.

它使用 Oauth 身份验证.

It uses Oauth authentication.

用户必须点击我网站上的一个按钮,然后被重定向到 Soundcloud 网站,并在那里登录.

The user has to click a button on my site and is redirected to the Soundcloud site, where it logs in.

之后,用户被重定向回我的网站,我必须在那里获取 accessToken.

After that the user get redirected back to my site where I have to get an accessToken.

失败并显示消息:请求的 URL 响应为 HTTP 代码 0.

如果你需要,我已经抛弃了 curl 的东西:

I've dumped the curl stuff if you need it:

 [data]; False
 [info][url]                    : https://api.soundcloud.com/oauth2/token
       [content_type]           ; NULL
       [http_code]              : 0
       [header_size]            : 0
       [request_size]           : 0
       [filetime]               : -1
       [ssl_verify_result]      : 0
       [redirect_count]         : 0
       [total_time]             : 0.031
       [namelookup_time]        : 0
       [connect_time]           : 0.031
       [pretransfer_time]       : 0
       [size_upload]            : 0
       [size_download]          : 0
       [speed_download]         : 0
       [speed_upload]           : 0
       [download_content_length]: -1
       [upload_content_length]  : -1
       [starttransfer_time]     : 0
       [redirect_time]          : 0
       [certinfo]               []

我执行的代码:

    $soundcloud = new SoundcloudModel('*****', '*****', '*****');

    try {
        $accessToken = $soundcloud->accessToken($this->request['code']);
    } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
        exit($e->getMessage());
    }

Soundcloud API 代码:

The Soundcloud API code:

protected function _getAccessToken($postData, $curlOptions = array())
{
    $options = array(CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData);
    $options += $curlOptions;
    $response = json_decode(
        $this->_request($this->getAccessTokenUrl(), $options),
        true
    );

    if (array_key_exists('access_token', $response)) {
        $this->_accessToken = $response['access_token'];

        return $response;
    } else {
        return false;
    }
}

protected function _request($url, $curlOptions = array())
{
    $ch = curl_init($url);
    $options = $this->_curlOptions;
    $options += $curlOptions;

    if (array_key_exists(self::CURLOPT_OAUTH_TOKEN, $options)) {
        $includeAccessToken = $options[self::CURLOPT_OAUTH_TOKEN];
        unset($options[self::CURLOPT_OAUTH_TOKEN]);
    } else {
        $includeAccessToken = true;
    }

    if (array_key_exists(CURLOPT_HTTPHEADER, $options)) {
        $options[CURLOPT_HTTPHEADER] = array_merge(
            $this->_buildDefaultHeaders(),
            $curlOptions[CURLOPT_HTTPHEADER]
        );
    } else {
        $options[CURLOPT_HTTPHEADER] = $this->_buildDefaultHeaders(
            $includeAccessToken
        );
    }

    curl_setopt_array($ch, $options);

    $data = curl_exec($ch);
    $info = curl_getinfo($ch);

    print_r(array('ch'=>$ch, 'data'=>$data, 'info'=>$info));

    curl_close($ch);

    $this->_lastHttpResponseHeaders = $this->_parseHttpHeaders(
        substr($data, 0, $info['header_size'])
    );
    $this->_lastHttpResponseBody = substr($data, $info['header_size']);
    $this->_lastHttpResponseCode = $info['http_code'];

    if ($this->_validResponseCode($this->_lastHttpResponseCode)) {
        return $this->_lastHttpResponseBody;
    } else {
        throw new Services_Soundcloud_Invalid_Http_Response_Code_Exception(
            null,
            0,
            $this->_lastHttpResponseBody,
            $this->_lastHttpResponseCode
        );
    }
}

对不起,这篇文章太长了,希望你们能帮帮我!

Sorry for this long post, hope you guys can help me out!

推荐答案

知道了.

我发现 PHP 使用的 cURL 使用了过时的证书包(至少在 Windows 上是这样).

I found out that cURL used by PHP uses an outdated cert bundle (at least on Windows).

我已经从以下位置下载了最新的证书包:http://curl.haxx.se/文档/caextract.html并添加了以下 cURL 选项:

I've downloaded the latest cert bundle from: http://curl.haxx.se/docs/caextract.html and added the following cURL option:

curl_setopt($ch, CURLOPT_CAINFO, 'C:/absolute/path/to/cacert.pem');

这篇关于Soundcloud (Oauth2) API 获取访问令牌失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆