执行对 ETSY 存储的请求,允许自动访问 PHP OAUTH [英] Performing requests to ETSY store allowing access automatically PHP OAUTH

查看:18
本文介绍了执行对 ETSY 存储的请求,允许自动访问 PHP OAUTH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用库连接到我的 ETSY 商店并从收据中提取数据以将它们带入我的个人网站(数据库).

I am using a library to connect to my ETSY store and pull data from receipts to bring them into my personal website (database).

使用 OAuth 发出请求后,我进入 ETSY 站点以允许访问"

After making the request using OAuth, I get to the ETSY site to "Allow Access"

https://www.etsy.com/images/apps/documentation/oauth_authorize.png

然后,我需要手动单击允许访问",我的请求将完成并显示所请求的数据.

Then, I need to manually click on Allow Access and my request will be completed and will display the data requested.

我想避免手动点击允许访问"的过程,因为我希望我的个人网站自动显示从 ETSY 订单中提取的信息.

I would like to avoid the process of manually clicking on "Allow Access", since I want my personal site to automatically display information pulled from ETSY orders.

这是我当前页面 etsyRequest.php 的代码:

Here is my current code for page etsyRequest.php:

    $credentials = new Credentials(
    $servicesCredentials['etsy']['key'],
    $servicesCredentials['etsy']['secret'],
    $currentUri->getAbsoluteUri()
);

// Instantiate the Etsy service using the credentials, http client and storage mechanism for the token
/** @var $etsyService Etsy */
$etsyService = $serviceFactory->createService('Etsy', $credentials, $storage);

if (!empty($_GET['oauth_token'])) {
    $token = $storage->retrieveAccessToken('Etsy');

    // This was a callback request from Etsy, get the token
    $etsyService->requestAccessToken(
        $_GET['oauth_token'],
        $_GET['oauth_verifier'],
        $token->getRequestTokenSecret()
    );

    // Send a request now that we have access token
    $result2 = json_decode($etsyService->request('/receipts/111111'));

    //echo 'result: <pre>' . print_r($result, true) . '</pre>';
    echo $result2->results[0]->seller_user_id;

如何通过运行此页面自动执行允许访问"部分并获取请求的返回值?

How could I automate the Allow Access part and get the returned value for my request by just running this page?

推荐答案

您只需保存返回的访问令牌"和令牌秘密"即可解决此问题.操作步骤:

You can resolved this problem by simply save the returned "access token" and "token secret". Steps to do it:

  • 使用 OAuth 发出请求后,您将访问 ETSY 站点以允许访问".允许后,它将显示一个 oauth_verifier 引脚.在您的代码中输入此 pin 后,它将设置访问令牌"并令牌秘密"到您的请求.您只需要将它们保存在变量或数据库.
  • 下次向 etsy 创建任何请求时,您只需设置这些访问令牌"和令牌秘密"与您的 oauth_consumer_key和 oauth_consumer_secret.那时您不需要 oauth_verifier 引脚.它会在您撤销 etsy 帐户的权限后起作用.

我在我的 java 代码中这样做是因为我面临同样的问题并且它的工作原理.(对不起,我在 php 方面不够好)这是我的示例代码,这可能会有所帮助-

I did this in my java code because i mm facing same problem and its working.(sorry i m not good enough in php) here is my sample code may this helps-

public void accessEtsyAccount(String consumer_key, String consumer_secret, String requestToken, String tokenSecret, String shopName) throws Throwable{

public void accessEtsyAccount(String consumer_key, String consumer_secret, String requestToken, String tokenSecret, String shopName) throws Throwable{

    OAuthConsumer consumer = new DefaultOAuthConsumer(
            consumer_key, consumer_secret
            );
    if(StringUtils.isBlank(requestToken) || StringUtils.isBlank(tokenSecret) ){
        OAuthProvider provider = new DefaultOAuthProvider(
                "https://openapi.etsy.com/v2/oauth/request_token",
                "https://openapi.etsy.com/v2/oauth/access_token",
                "https://www.etsy.com/oauth/signin");

        System.out.println("Fetching request token from Etsy...");

        // we do not support callbacks, thus pass OOB
        String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
        System.out.println("Request token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());
        System.out.println("Now visit:\n" + authUrl
                + "\n... and grant this app authorization");
        if(Desktop.isDesktopSupported()){
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.browse(new URI(authUrl));
            } catch (IOException | URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("xdg-open " + authUrl);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("Enter the PIN code and hit ENTER when you're done:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String pin = br.readLine();

        System.out.println("Fetching access token from Etsy...");

        provider.retrieveAccessToken(consumer, pin);
    } else {
        consumer.setTokenWithSecret(requestToken, tokenSecret);

    }
        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        URL url = new URL("https://openapi.etsy.com/v2/private/shops/"+shopName+"/transactions");

        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request to Etsy...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage());

        System.out.println("Payload:");
        InputStream stream = request.getInputStream();
        String stringbuff = "";
        byte[] buffer = new byte[4096];

        while (stream.read(buffer) > 0) {
            for (byte b: buffer) {
                stringbuff += (char)b;
            }
        }

        System.out.print(stringbuff);

这篇关于执行对 ETSY 存储的请求,允许自动访问 PHP OAUTH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆