Get OAuth Ebay access token nigthmare MediaAPI

I don’t know how, one of the biggest ecommerce platforms, has sparse, poor and bad documentation. I have seen api’s built by small tech companies and they are a hell of a lot better than this one.

I’m struggling creating a new access token for the media api (I need the product videos) So, after wasting a lot of time I just finally get the “code” via url and its “new” OAuth2. With this I get the access token and with this I can be able to get the info of a video by the videoID parameter of getItem endpoint.

My problem is this. I need to update programmatically my woocommerce website. I desing a script to get the items, save to a cvs file and import via wp all import, but I need to get the videos urls and I can get only the “code” param open a new window and needs the human intervention. If I use the access token to get the video for 1 product and if I use the same token to a new product then ebay returns me an error like: “The access token is invalid or has already use it” (say something like this)

How can I get the access token or improve this process to avoid human intervention??? or maybe one every 18 months that it’s suppose to live the refresh token.

This is my dummy code, this is only for testing purpose:

<?php
$oauth_clientIdfile  = 'AAAAAAAAAAAAAA';     // file containing appID  = clientID

$client_id = $oauth_clientIdfile;
$oauth_secretIdfile  = 'BBBBBBBBB';   // file containing certID = clientSecret
$client_secret = $oauth_secretIdfile;
$redirect_uri = 'https://www.example.com/callback.php';
$response_type = 'code';
$scope = urlencode('https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.inventory');


if (isset($_GET['code'])) {
  // Step 2: Handle the callback and exchange the authorization code for an access token
  $auth_code = $_GET['code'];

  $token_url = "https://api.ebay.com/identity/v1/oauth2/token";

  $post_fields = http_build_query([
    'grant_type' => 'authorization_code',
    'code' => $auth_code,
    'redirect_uri' => $redirect_uri
  ]);

  $headers = [
    "Content-Type: application/x-www-form-urlencoded",
    "Authorization: Basic " . base64_encode("$client_id:$client_secret")
  ];

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $token_url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  $response = curl_exec($ch);

  if ($response === false) {
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
  }

  curl_close($ch);

  $response_data = json_decode($response, true);

  if (isset($response_data['access_token'])) {
    $access_token = $response_data['access_token'];
    echo "Access Token: " . htmlspecialchars($access_token);

    // You can now use the access token to make API calls
    // Example API call to getVideo endpoint
    $video_id = 'EEEEEEEEEEEEEEEEEEE';
    $api_url = "https://apim.ebay.com/commerce/media/v1_beta/video/$video_id"; // getVideo endpoint

    $api_headers = [
      "Authorization: Bearer $access_token",
      "Content-Type: application/json",
      // "Content-Language: en-US",
      // "Accept: application/json",
      // "Accept-Encoding: gzip",
      // "Accept-Language: en-US",
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $api_headers);

    $api_response = curl_exec($ch);
    // echo "<pre>*";
    // print_r($api_response);
    // echo "*</pre>";

    if ($api_response === false) {
      die('API Request Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
    }

    curl_close($ch);
    // echo "API Response: " . htmlspecialchars($api_response);
    $response_data = json_decode($api_response, true);
    echo "API Response: ";
    echo "<pre>*";
    print_r($response_data);
    echo "*</pre>";
  } else {
    echo "Error retrieving access token: " . htmlspecialchars($response);
  }
} else {
  // Step 1: Redirect to eBay's authorization URL
  $auth_url = "https://auth.ebay.com/oauth2/authorize?client_id={$client_id}&redirect_uri=" . urlencode($redirect_uri) . "&response_type=code&scope=" . urlencode($scope);

  header("Location: $auth_url");
  exit;
}

The code above works if I manually run open the page in a browser, but I need to run this process in a cron job.

Is there another way to get token to get the video info???

Thanks a lot for all your help. Your guidance!!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật