1

我已按照 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;

?>

4

1 回答 1

-2

好的,我想通了,我只是不知道如何解释,但有代码

<?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";
    }
}

?>

于 2018-05-25T03:09:04.003 回答