Credentials flow #11

Merged
merged 2 commits into from Jul 17, 2012

Projects

None yet

2 participants

@artwist
Contributor
artwist commented Jul 3, 2012

Usage:

<?php

$client = new Services_Soundcloud(CLIENT_ID, CLIENT_SECRET);

$token = json_decode($client->credentialsFlow(USERNAME, PASSWORD));

$client->setAccessToken($token->access_token);

$user = json_decode($client->get('me'));
@mptre mptre commented on an outdated diff Jul 4, 2012
Services/Soundcloud.php
+ *
+ * @return mixed
+ *
+ * @access public
+ */
+ function credentialsFlow($username, $password)
+ {
+ $postData = array(
+ 'client_id' => $this->_clientId,
+ 'client_secret' => $this->_clientSecret,
+ 'username' => $username,
+ 'password' => $password,
+ 'grant_type' => 'password'
+ );
+
+ return $this->_request($this->getAccessTokenUrl(), array(
mptre
mptre Jul 4, 2012 Owner

Try to mimic the flow from the _getAccessTokenmethod.

Make sure to parse the response and assign the returned access token.

<?php
$options = array(
    CURLOPT_POST => true, 
    CURLOPT_POSTFIELDS => $postData
);
$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;
}
@mptre mptre commented on an outdated diff Jul 4, 2012
Services/Soundcloud.php
+ * @return mixed
+ *
+ * @access public
+ */
+ function credentialsFlow($username, $password)
+ {
+ $postData = array(
+ 'client_id' => $this->_clientId,
+ 'client_secret' => $this->_clientSecret,
+ 'username' => $username,
+ 'password' => $password,
+ 'grant_type' => 'password'
+ );
+
+ return $this->_request($this->getAccessTokenUrl(), array(
+ CURLOPT_POST => true,
mptre
mptre Jul 4, 2012 Owner

Don't align assoc array values.

@mptre mptre commented on an outdated diff Jul 4, 2012
Services/Soundcloud.php
+ *
+ * @access public
+ */
+ function credentialsFlow($username, $password)
+ {
+ $postData = array(
+ 'client_id' => $this->_clientId,
+ 'client_secret' => $this->_clientSecret,
+ 'username' => $username,
+ 'password' => $password,
+ 'grant_type' => 'password'
+ );
+
+ return $this->_request($this->getAccessTokenUrl(), array(
+ CURLOPT_POST => true,
+ CURLOPT_POSTFIELDS => $postData,
mptre
mptre Jul 4, 2012 Owner

No need for the trailing comma.

@mptre mptre merged commit ee15265 into mptre:master Jul 17, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment