Performing requests to ETSY store allowing access automatically PHP OAUTH

我的梦境 提交于 2021-02-07 19:54:40

问题


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

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.

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.

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?


回答1:


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

  • After making the request using OAuth, you get to the ETSY site to "Allow Access". after allowing it will show a oauth_verifier pin. After you enter this pin in your code it will set "access token" and "token secret" to your request.you just need to save them in variables or database.
  • next time when to create any request to etsy you just have to set these access token" and "token secret" with your oauth_consumer_key and oauth_consumer_secret. you don't need oauth_verifier pin at that time. it will work util you revoke permission from your etsy account.

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{

    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);



回答2:


You need to save the access token when you have requested the Etsy store for the first time and then the same access token can be used for later calls. This would prevent you from clicking ALLOW ACCESS again and again when requesting Etsy store through API.



来源:https://stackoverflow.com/questions/21761884/performing-requests-to-etsy-store-allowing-access-automatically-php-oauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!