Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to refresh token in Reddit Api with basic auth? #31

Closed
bhargavjoshi opened this issue Feb 26, 2014 · 3 comments
Closed

How to refresh token in Reddit Api with basic auth? #31

bhargavjoshi opened this issue Feb 26, 2014 · 3 comments

Comments

@bhargavjoshi
Copy link

I am using you library for performing authentication on reddit and posting on reddit but i can't refresh the token using your library.

To refresh token i use the following code, using this library (https://github.com/php-curl-class/php-curl-class)

 $accessTokenUrl = 'https://ssl.reddit.com/api/v1/access_token';
    $clientId = 'ClientID';
    $clientSecret = 'ClientSecret';
    $redirectUrl = "YourURL";

    $string = $clientId.":".$clientSecret;
    $val = base64_encode($string);
    $val = " Basic ".$val;

    $curl = new Curl();
    $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
    $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);

    $curl->setHeader('Authorization', $val);

    $curl->post($accessTokenUrl, array(
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'grant_type' => 'refresh_token',
        'refresh_token' => $refresh_token,
        'scope' => 'identity,edit,submit',
        'state' => 'refresh_token',
        'duration' => 'permanent',
        'redirect_uri' => $redirectUrl
    ));
    if (!$curl->error) {
        if ($curl->http_status_code == 200){
            $resp_data = json_decode($curl->response, 1);
            $data = array('error' => 0, 'data' => $resp_data);
            return $data;
        }else{
            $data = array('error' => 1);
            return $data;
        }
    }else{
        $data = array('error' => 1);
        return $data;
    }

Note: the api requires a Basic auth with client id as username and client secret as password.

It would be really great if you can help me implement refresh token with this library.

@bhargavjoshi
Copy link
Author

Sorry for opening ticket issue is solved.

$accessTokenUrl = 'https://ssl.reddit.com/api/v1/access_token';
$client = new OAuth2\Client($clientId, $clientSecret,
OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC);
$response = $client->getAccessToken($accessTokenUrl,
OAuth2\Client::GRANT_TYPE_REFRESH_TOKEN,array(
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'grant_type' => 'refresh_token',
        'refresh_token' => $refresh_token,
        'scope' => 'identity,edit,submit',
        'state' => 'refresh_token',
        'duration' => 'permanent',
        'redirect_uri' => $redirectUrl
    ));

@adoy adoy closed this as completed Mar 7, 2014
@ThanhUong
Copy link

Hi bhargavjoshi, I am trying to implement the refresh token example in your latest comment. Can you please let me know what the $refresh_token variable should be set to? I don't see it in defined in your example.

@techguydev
Copy link

If anyone is interested in this website you can find a nice implementation to refresh the token automatically http://www.blotdesign.com/article-detail.php?page=133

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants