ผู้บริโภคไม่ได้รับอนุญาตให้เข้าถึงทรัพยากร% สำหรับ API ส่วนที่เหลือของ Magento2


9

ฉันสร้างลูกค้าใหม่และได้รับโทเค็นอย่างสมบูรณ์แบบ แต่เมื่อฉันพยายามเข้าถึง API เช่นนั้น

http://myhost.com/index.php/rest/V1/customers/me?Authorization=Bearerhy23vc01x9s1jd2t8gho47g58trllc0b 

ได้รับข้อผิดพลาด

<?xml version="1.0"?>
<response>
<message>Consumer is not authorized to access %resources</message>
<parameters>
<resources>self</resources>
</parameters>
  <trace>#0 /var/www/myhost.com/public_html/vendor/magento/module-webapi/Controller/Rest/RequestValidator.php(70): Magento\Webapi\Controller\Rest\RequestValidator-&gt;checkPermissions()
#1 /var/www/myhost.com/public_html/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php(80): Magento\Webapi\Controller\Rest\RequestValidator-&gt;validate()
#2 /var/www/myhost.com/public_html/vendor/magento/module-webapi/Controller/Rest.php(299): Magento\Webapi\Controller\Rest\InputParamsResolver-&gt;resolve()
#3 /var/www/myhost.com/public_html/vendor/magento/module-webapi/Controller/Rest.php(216): Magento\Webapi\Controller\Rest-&gt;processApiRequest()
#4 /var/www/myhost.com/public_html/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest-&gt;dispatch(Object(Magento\Framework\App\Request\Http))
#5 /var/www/myhost.com/public_html/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor-&gt;dispatch(Object(Magento\Framework\App\Request\Http))
#6 /var/www/myhost.com/public_html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http-&gt;launch()
#7 /var/www/myhost.com/public_html/index.php(39): Magento\Framework\App\Bootstrap-&gt;run(Object(Magento\Framework\App\Http))
#8 {main}</trace>
</response>

กรุณาช่วยใด ๆ

คำตอบ:


10

โทเค็นการอนุญาตไม่ได้อยู่ในพารามิเตอร์ ควรตั้งไว้ที่ส่วนหัวแทน นี่คือตัวอย่างของ POSTMAN:

ป้อนคำอธิบายรูปภาพที่นี่

บน cURL จะเป็น:

curl -X GET --header "Accept: application/json" --header "Authorization: Bearer XXXXXXXXXX" "http://yoursite.com/rest/V1/products/:sku"

ขอบคุณสำหรับการตอบกลับของคุณฉันได้ลองใช้กับส่วนหัวหรือเป็นพารามิเตอร์ใน url และทำงานได้อย่างสมบูรณ์แบบสำหรับ API บางอย่างproductsที่คุณทำและcategoriesสำหรับ API บางตัวV1/customers/meฉันได้รับข้อผิดพลาด แม้ว่าฉันจะเรียกใช้ curl บน terminal มันทำงานได้อย่างสมบูรณ์ ฉันใช้บุรุษไปรษณีย์และผู้ร้องขอ http สำหรับ firefox
Jsparo30

ไม่มีปัญหาในเว็บไซต์ทดสอบของฉัน คุณมีช่องว่างระหว่าง "ผู้ถือ" และโทเค็นหรือไม่? และบางทีโทเค็นของคุณอาจหมดอายุลองหาใหม่
PY Yick

HHH เป็นเรื่องตลกอะไร !! ไม่มีที่ว่างและทำงานได้ดีproductsและcategoriesอย่างที่ฉันพูดถึง แต่เมื่อลบออกในที่customers/meทำงานได้ดี โปรดแก้ไขคำตอบและเพิ่มส่วนของพื้นที่ว่างเพื่อให้เป็นคำตอบที่แท้จริง ขอบคุณ
Jsparo30

ที่จริงแล้วมีแนวโน้มที่โทเค็นจะหมดอายุ ฉันใช้โทเค็นเก่าเพื่อเข้าถึง API มันแสดงข้อผิดพลาดเดียวกับที่คุณระบุไว้ในคำถาม เพียงต่ออายุโทเค็นและแก้ไขปัญหาได้แล้ว
PY Yick

@ Jsparo30 คุณพบวิธีการแก้ปัญหาฉันยังเผชิญปัญหาเดียวกัน
Janarthanan Ramu
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.