Etsy PHP Oauth: не могу получить токен доступа - PullRequest
0 голосов
/ 25 мая 2018

Я следовал etsy-руководству для аутентификации моего приложения и подключения к пользователю, и мне удалось пройти первый процесс, чтобы получить oauth_token, секретный токен и верификатор.Но после установки токена для oauth происходит сбой функции getAccessToken с этим сообщением

Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)bad

Это мой код, так как вы можете сказать, что я перепробовал много опций, моя конечная цель - сохранить все учетные данные в файле.в базе данных, но сначала я хочу узнать, что не так с моим приложением

<?php       
header('Content-type: text/plain');
ini_set('max_execution_time', 600);
$ksecrFile = fopen("key_secret.txt", "r") or die("Unable to open file!");
$key = trim(fgets($ksecrFile),"\n");
$secret = trim(fgets($ksecrFile),"\n");
$verifier = "";
fclose($ksecrFile);

$lines = file("key_secret.txt");

$oauth = new OAuth($key, $secret);
//$oauth->setAuthType(OAUTH_AUTH_TYPE_URI);
$oauth->disableSSLChecks();


function getToken($oauth, $verifier){

    $req_token = $oauth->getRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r",  "http://localhost/ksec.php");

    if (!empty($_GET))
    {
        print_r($req_token);
        $verifier = $_GET["oauth_verifier"];
        $token =  $req_token['oauth_token'];
        $token_secret = $req_token['oauth_token_secret'];
        //$tokenFile = fopen("token.txt", "w");
        //fwrite($tokenFile, $verifier . "\r\n");
        //fwrite($tokenFile, $token . "\r\n");
        //fwrite($tokenFile, $token_secret);
        //fclose($tokenFile);
        //header("Location: http://localhost/ksec.php");
        echo $verifier . " " . $token . " " . $token_secret . "\n";
        $ksecrFile = fopen("key_secret.txt", "r") or die("Unable to open file!");
        $key = trim(fgets($ksecrFile),"\n");
        $secret = trim(fgets($ksecrFile),"\n");
        $oauth1 = new OAuth($key, $secret);
        $oauth1->disableSSLChecks();
        $oauth1->setToken($req_token['oauth_token'], $req_token['oauth_token_secret']);

        try {
            // set the verifier and request Etsy's token credentials url
            $acc_token = $oauth1->getAccessToken("https://openapi.etsy.com/v2/oauth/access_token", null, $_GET["oauth_verifier"]);
            echo "good";
        } catch (OAuthException $e) {
            print_r($e->getMessage());
            echo "bad";
        }
    }
    else
    {
        $login_url = sprintf(
            "%s?oauth_consumer_key=%s&oauth_token=%s",
            $req_token['login_url'],
            $req_token['oauth_consumer_key'],
            $req_token['oauth_token']
        );

        header("Location: " . $login_url);
    }
}

$tokenFile = fopen("token.txt", "r") or die(getToken($oauth, $verifier));
//$verifier = trim(fgets($tokenFile),"\n");
//$token = trim(fgets($tokenFile),"\n");
//$tokenSecret = trim(fgets($tokenFile),"\n");
//fclose($tokenFile);

//echo $verifier . " " . $token . " " . $tokenSecret . "\n";



//echo $verifier . " " . $token;

?>

1 Ответ

0 голосов
/ 25 мая 2018

Хорошо, я понял это, я просто не знаю, как объяснить, но с кодом

<?php   
header('Content-type: text/plain');
ini_set('max_execution_time', 600);
$ksecrFile = fopen("key_secret.txt", "r") or die("Unable to open file!");
$key = trim(fgets($ksecrFile),"\n");
$secret = trim(fgets($ksecrFile),"\n");
fclose($ksecrFile);

$oauth = new OAuth($key, $secret);
$oauth->disableSSLChecks();
$tokenFile = fopen("token.txt", "r") or die(getToken($oauth, $key, $secret));

function getToken($oauth, $key, $secret){

    $req_token = $oauth->getRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r",  "http://localhost/ksec.php");
    $tokenFile = fopen("token.txt", "w") or die("Unable to open file!");
    fwrite($tokenFile, $req_token['oauth_token'] . "\n");
    fwrite($tokenFile, $req_token['oauth_token_secret'] . "\n");

    $login_url = sprintf(
        "%s?oauth_consumer_key=%s&oauth_token=%s",
        $req_token['login_url'],
        $req_token['oauth_consumer_key'],
        $req_token['oauth_token']
    );

    header("Location: " . $login_url);
}

if (empty($_GET))
{
    getToken($oauth, $key, $secret);
}
else
{
    $tokenFile = fopen("token.txt", "r") or die("Unable to open file!");
    $token = trim(fgets($tokenFile),"\n");
    $tokenSecret = trim(fgets($tokenFile),"\n");
    fclose($tokenFile);

    $oauth1 = new OAuth($key, $secret);
    $oauth1->disableSSLChecks();
    $oauth1->setToken($token, $tokenSecret);

    try {
        // set the verifier and request Etsy's token credentials url
        $acc_token = $oauth1->getAccessToken("https://openapi.etsy.com/v2/oauth/access_token", null, $_GET["oauth_verifier"]);
        echo "good";
    } catch (OAuthException $e) {
        print_r($e->getMessage());
        echo "bad";
    }
}

?>

...