开发者

Soundcloud (Oauth2) API getting access token fails

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

It uses Oauth authentication.

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

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

This fails with message: The requested URL responded with HTTP code 0.

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_开发者_如何学Ctime]          : 0
       [certinfo]               []

The code I execute:

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

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

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!


Got it.

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

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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜