Let me just preface this by restating this error does not occur in either my local or online beta environment, only in production; despite all 3 servers sharing the same following environment variables:
REDDIT_USERNAME=username
REDDIT_PASSWORD=pwd
REDDIT_ID=id
REDDIT_SECRET=secret
When I try to request a reddit access token in production using this method:
/**
* @internal
*
* Request A Reddit Token
*
* If the client does not have a current valid OAuth2 token, fetch one here. Set it as a cookie.
*/
private function requestRedditToken() {
$response = $this->client->post(Reddit::ACCESS_TOKEN_URL, array(
'query' => [
[
'client_id' => $this->clientId,
'response_type' => 'code',
'state' => bin2hex(openssl_random_pseudo_bytes(10)),
'redirect_uri' => 'http://localhost/reddit/test.php',
'duration' => 'permanent',
'scope' => 'save,modposts,identity,edit,flair,history,modconfig,modflair,modlog,modposts,modwiki,mysubreddits,privatemessages,read,report,submit,subscribe,vote,wikiedit,wikiread'
]
],
'auth' => [$this->clientId, $this->clientSecret],
'form_params' => [
'grant_type' => 'password',
'username' => $this->username,
'password' => $this->password
]
));
$body = json_decode($response->getBody());
$this->tokenType = $body->token_type;
$this->accessToken = $body->access_token;
// Set the cookie to expire in 60 minutes.
setcookie('reddit_token', $this->tokenType . ':' . $this->accessToken, 60 * 60 + time());
}
I get an exception which breaks the flow of my app because $response->getBody()
is equal to invalid_grant
.
Yet, I can't figure out why it is invalid, since using the precise similar credentials in different environments, and even just using Postman, result in me receiving a valid access token in response. What's going on here?