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

查看:73
本文介绍了执行对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引脚.在代码中输入此密码后,它将设置访问令牌",您的请求的令牌秘密".您只需将它们保存在变量或数据库.
  • 下一次何时创建对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)引发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天全站免登陆