0

让我先重申一下这个错误不会发生在我的本地或在线测试环境中,仅在生产中;尽管所有 3 台服务器共享相同的以下环境变量:

REDDIT_USERNAME=username
REDDIT_PASSWORD=pwd
REDDIT_ID=id
REDDIT_SECRET=secret

当我尝试使用此方法在生产中请求 reddit 访问令牌时:

/**
 * @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());
}

我得到一个异常,它破坏了我的应用程序的流程,因为$response->getBody()等于invalid_grant.

然而,我无法弄清楚为什么它是无效的,因为在不同的环境中使用精确的类似凭据,甚至只是使用 Postman,都会导致我收到一个有效的访问令牌作为响应。这里发生了什么?

4

0 回答 0