我已按照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;
&GT;
答案 0 :(得分:0)
好的,我想通了,我只是不知道如何解释,但有代码
<?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";
}
}
&GT;