Magento POST requests throwing 403 - php

I've a magento 1.9.1 community instance setup and I'm trying to access them via REST API . I'm able to perform GET requests for Products/Customers etc but I'm stuck while adding a product using a POST request. My sample code is below. Where am I going wrong ?
from requests_oauthlib import OAuth1 as OAuth
import requests
import json
oauth = OAuth(client_key=consumer_key,
client_secret=consumer_secret,
resource_owner_key=access_key,
resource_owner_secret=access_secret)
headers = {'Content-Type': 'application/json',
'Accept': 'application/json'}
product = """{
"type_id": "simple",
"attribute_set_id": "4",
"sku": "Test",
"name": "Test",
"price": "100",
"description": "Random description",
"short_description": "Random",
"weight": "500",
"status": "1",
"visibility": "4",
"tax_class_id": "2"
}"""
payload = json.loads(product)
uri = "http://magentohost/api/rest/products"
r = requests.post(url=uri, data=json.dumps(payload),headers=headers, auth=oauth)
print r

Related

Send Current URL link to A Discord Webhook

So, basically I need a way to send the current URL link to A Discord webhook.
So for example, the URL would be https://www.example.com and I need that link to send to my Discord webhook.
Is there any way someone could help me with this? If this is not possible, are there any alternative methods?
I would recommend you first reading this—it explains in detail how to format the payload so that it renders as a message in Discord properly, you can even use markdown!
Second, I'm not sure what language you want to code this in, but here's a simple example using Python and httpx
The idea to take away here is
Pick a client library
Study the Discord webhook message format
Create a client for HTTP
Set the logic to send the payload to the webhook you want
Create and send the payload with the formatted webhook message in a POST request
data = {
"username": "Webhook",
"avatar_url": "https://i.imgur.com/4M34hi2.png",
"content": "Text message. Up to 2000 characters.",
"embeds": [
{
"author": {
"name": "Birdie♫",
"url": "https://www.reddit.com/r/cats/",
"icon_url": "https://i.imgur.com/R66g1Pe.jpg"
},
"title": "Title",
"url": "https://google.com/",
"description": "Text message. You can use Markdown here. *Italic* **bold** __underline__ ~~strikeout~~ [hyperlink](https://google.com) `code`",
"color": 15258703,
"fields": [
{
"name": "Text",
"value": "More text",
"inline": true
},
{
"name": "Even more text",
"value": "Yup",
"inline": true
},
{
"name": "Use `\"inline\": true` parameter, if you want to display fields in the same line.",
"value": "okay..."
},
{
"name": "Thanks!",
"value": "You're welcome :wink:"
}
],
"thumbnail": {
"url": "https://upload.wikimedia.org/wikipedia/commons/3/38/4-Nature-Wallpapers-2014-1_ukaavUI.jpg"
},
"image": {
"url": "https://upload.wikimedia.org/wikipedia/commons/5/5a/A_picture_from_China_every_day_108.jpg"
},
"footer": {
"text": "Woah! So cool! :smirk:",
"icon_url": "https://i.imgur.com/fKL31aD.jpg"
}
}
]
}
import httpx # pip install httpx, or poetry add httpx
with httpx.Client() as client:
request = client.post("https://www.discordwebhook.com", data=data) # needs to be a POST
print(request.status_code)

PHP Paypal MALFORMED_REQUEST_JSON although JSON seems to be well formatted

I try to implement a paypal express checkout flow to a website.
The user must be able to adjust the order after they have logged in with paypal and choosed their payment option.
To do so, I create the order with paypal with the intent "AUTHORIZE" and user action "CONTINUE". It is created, the user is sent back to our website and I can fetch order and payer information with the (order) id created.
But then, when the payer is done checking and adjusting some final parameters and clicks the "buy now" button, I want to send the update order call as referenced here:
https://developer.paypal.com/api/orders/v2/#orders_patch
I create the payload in exactly the same way as for order creation, but this time with intent "CAPTURE" and user_action="PAY_NOW". I PATCH it to the correct path, but I keep getting the failure message "MALFORMED_REQUEST_JSON" .
Hoewever, the JSON itself is a valid JSON, no errors thrown in creation. Here is a sample:
{
"intent": "CAPTURE",
"application_context": {
"landing_page": "NO_PREFERENCE",
"shipping_preference": "SET_PROVIDED_ADDRESS",
"user_action": "PAY_NOW"
},
"purchase_units": [{
"reference_id": "2289256",
"description": "Your order at site",
"custom_id": "order id 2289256",
"soft_descriptor": "site name",
"invoice_id": "2289256",
"amount": {
"currency_code": "EUR",
"value": 59.98,
"breakdown": {
"item_total": {
"currency_code": "EUR",
"value": 50.41
},
"shipping": {
"currency_code": "EUR",
"value": 0
},
"discount": {
"currency_code": "EUR",
"value": 0
},
"tax_total": {
"currency_code": "EUR",
"value": 9.57
}
}
},
"items": [{
"name": "Product 1",
"description": "Product 1 Description",
"sku": "1019879",
"unit_amount": {
"currency_code": "EUR",
"value": 16.8
},
"tax": {
"currency_code": "EUR",
"value": 3.19
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
}, {
"name": "Product 2",
"description": "Product 2 Description",
"sku": "1024593",
"unit_amount": {
"currency_code": "EUR",
"value": 33.61
},
"tax": {
"currency_code": "EUR",
"value": 6.38
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
}],
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "Badensche Str. 24",
"address_line_2": "",
"admin_area_2": "Berlin(Berlin)",
"postal_code": "10715",
"country_code": "DE"
}
}
}]
}
Here is paypal´s response
{
name: "INVALID_REQUEST",
message: "Request is not well-formed, syntactically incorrect, or violates schema.",
debug_id: "c315ce9eb90b4",
details: [{
field: "/",
location: "body",
issue: "MALFORMED_REQUEST_JSON",
description: "The request JSON is not well formed.",
}],
links: [{
href: "https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON",
rel: "information_link",
encType: "application/json",
}],
}
I just cannot figure out the problem. I tried to remove the whole application_context, purchase_units, intent and see if there is a problem within any of these parameters. Nothing changed. What exactly is wrong with this call?
That's not how a patch operation works. A patch operation's JSON payload must look something like the example in the API reference:
'[
{
"op": "replace",
"path": "/purchase_units/#reference_id==\'default\'/shipping/address",
"value": {
"address_line_1": "123 Townsend St",
"address_line_2": "Floor 6",
"admin_area_2": "San Francisco",
"admin_area_1": "CA",
"postal_code": "94107",
"country_code": "US"
}
}
]'
However, based on the information provided it's unclear why you are attempting to use a PATCH, since you do not list any relevant fields for a patch.
Intent authorize and intent capture are for creating an order. Use one or the other, never both for the same transaction. To capture an order that was created with intent capture, use a capture API call. To authorize an order that was created with intent authorize, use an authorize API call. The relevant API endpoints for either will be in the API response when you create the order, or you can read the documentation.
Since it appears your intended result is a captured payment (rather than an authorization), intent authorize is not relevant to your use case.

Laravel Authentication with external API

I have an external server API made with node JS and mongo. I suppose user API is localhost:8010/api/login output how do I use that for auth in laravel ?
"users": [
{
"_id": "592e71bc05be3c26f6bda51d",
"updatedAt": "2017-05-31T07:33:16.608Z",
"createdAt": "2017-05-31T07:33:16.608Z",
"userToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiRW1qb3kiLCJkYXRlIjoxNDk2MjE1OTk2NTk0fQ.25AY8EGoCmpcToidVWBU_czOKCFbV-dkrmCPUx6InD4",
"name": "desert",
"mobile": "123456789",
"type": "N",
"is_active": 1,
"__v": 0
}
Auth::login method will see your desired job
$model = User::find(1);
Auth::login($model, true);

Get eventbrite attendes of a particular order id with personal oauth token

I am trying to integrate eventbrite with infusionsoft. I used eventbrite webhook to get the order api_url once an order is placed on my event. Using that api_url and personal oauthtoken, i am able to get the details of the particular order by passing the token "api_url?token=xxxxx"
This is the json response of the webhook after the order is placed
{"config": {"action": "order.placed", "user_id": "xxxxx", "endpoint_url":"http://example.com", "webhook_id": "xxxx"}, "api_url": "https://www.eventbriteapi.com/v3/orders/xxxx/"}
I am passing the personal token to the order api_url "https://www.eventbriteapi.com/v3/orders/xxxx?token=xxxx"
The json response got from the above is
{
"resource_uri": "https://www.eventbriteapi.com/v3/orders/xxxx/",
"id": "xxxxx",
"changed": "2015-09-09T09:41:31Z",
"created": "2015-09-09T09:41:30Z",
"costs": {
"payment_fee": {
"currency": "GBP",
"display": "\u00a30.00",
"value": 0
},
"gross": {
"currency": "GBP",
"display": "\u00a30.00",
"value": 0
},
"eventbrite_fee": {
"currency": "GBP",
"display": "\u00a30.00",
"value": 0
},
"tax": {
"currency": "GBP",
"display": "\u00a30.00",
"value": 0
},
"base_price": {
"currency": "GBP",
"display": "\u00a30.00",
"value": 0
}
},
"name": "test67 test67",
"first_name": "test67",
"last_name": "test67",
"email": "test67#gmail.com",
"status": "placed",
"time_remaining": null,
"event_id": "xxxx"
}
Following is the code i am using to retrieve the json response
$url = $api_url.'?token=xxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
The problem is it only gives the basic information of the event. It does not give the attendees information in response. Can you help me with an idea or example to retrieve the attendees list belonging to a specific order id?
And is it possible to get the attendees list only using the personal oauth token?

Facebook - Get & Display me/og.likes in Video application

Have built an video app that publish user actions towards Facebook.
In this app i have implemented an "Favorite" function that i have hooked up towards a basic open graph action "og.like"
I want to be able to display video's that user liked and apply my own styling to that.
Basically i want to display "Title" "Url" & "Image"
So i use the PHP-SDK towards authored user with active access token and execute
$response = $facebook->api(
'me/og.likes',
'GET'
);
// handle the response
How do i now sort out my correct fields and display them ?
Am not hardcore at either php or javascript but will be able to sort this out if i just can get a little push in the right direction. Like just showing the raw data
Update
Finally a little progress, adding
print_r ($response);
Will write out the raw data, Now i know that am on the right way.
Array returned
{
"data": [
{
"id": "123",
"from": {
"name": "Mathias",
"id": "APP_ID"
},
"start_time": "X",
"end_time": "X",
"publish_time": "X",
"application": {
"name": "APP_Name",
"namespace": "",
"id": "321"
},
"data": {
"object": {
"id": "139",
"url": "Url to like",
"type": "video.tv_show",
"title": "title"
}
},
"type": "og.likes",
"no_feed_story": false,
"likes": {
"count": 0,
"can_like": true,
"user_likes": false
},
"comments": {
"count": 0,
"can_comment": true,
"comment_order": "chronological"
}
},
And then the next..
From every app "like" i would like to display Url ,Title & Image
From what i understand so far my main problem is that this is nested arrays, Did try with single level arrays and there i did manage to display correct data just by
echo $response[name];
So how do i digg in and loop this around, All tips are welcome,
{
"id": "139",
"url": "url",
"type": "video.tv_show",
"title": "titke",
"image": [
{
"url": "image_URL",
"secure_url": "image_URL",
"type": "image/jpg",
"width": 1024,
"height": 576
}
Here's an example:
<?php foreach ( $response['data'] as $data ): ?>
<?php $Object = $data['data']['object']; ?>
<?php echo $Object['title']; ?><br />
<?php endforeach; ?>

Categories