PHP 5.4 Implementation of the Discogs API - php

In PHP 5.4 Implementation of the Discogs API (https://github.com/ricbra/php-discogs-api) there is example how to get $response = $client->search(['q' => 'Meagashira']); working with oauth1. The example states:
$oauth = new GuzzleHttp\Subscriber\Oauth\Oauth1([
'consumer_key' => $consumerKey, // from Discogs developer page
'consumer_secret' => $consumerSecret, // from Discogs developer page
'token' => $token['oauth_token'], // get this using a OAuth library
'token_secret' => $token['oauth_token_secret'] // get this using a OAuth library
]);
The consumer_key and consumet_secret is given in the discogs app setting. How do I get token and token_secret? I try to make this example code working from command line but I get bunch of exceptions mostly:
PHP Fatal error: Uncaught exception
'GuzzleHttp\Exception\ClientException' with message 'Client error
response [url] http://api.discogs.com/database/search?q=Metallica
[status code] 401 [reason phrase] Unauthorized' in
/var/www/darkplanet/vendors/composer/guzzlehttp/guzzle/src/Exception/RequestException.php:

Disclaimer: I'm the author of the Discogs API PHP client.
Recently I've updated the README.md with a link to an example implementation which can be used as starting point. You can find this example implementation at: https://github.com/ricbra/php-discogs-api-example

Related

How to make a call with the Twitter V2 API

I'm using the below code to search Tweets with the query parameter. This works fine with the Twitter V1 API, but not with the V2, and after extensively reading their API documentation, I'm confused.
My first question is, do the V1 and V2 share the same authentication? i.e. can I make a call to the V2 with this code, assuming my end point url is correct which leads to the second question, if I can make a call to the V2 with the same auth as the V1, what's the correct end point? the notable differences so far is the query parameter which is "q" for the V1 and "query" for the V2. Then apparently the .json is not needed, but I tried both at the end of the V2 end point and still I get the same error:
stdClass Object ( [client_id] => 131XXXXXX [detail] => When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal. [registration_url] => https://developer.twitter.com/en/docs/projects/overview [title] => Client Forbidden [required_enrollment] => Standard Basic [reason] => client-not-enrolled [type] => https://api.twitter.com/2/problems/client-forbidden )
include ($_SERVER["DOCUMENT_ROOT"] . "/TwitterAPIExchange.php");
$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);
//$url = "https://api.twitter.com/1.1/search/tweets.json";
$url = "https://api.twitter.com/2/tweets/search/recent";
$requestMethod = "GET";
// V1
//$getfield = '?q=bitcoin';
// V2
$getfield = '?query=bitcoin';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($response);
print_r($tweets);
EDIT:
Looks like the V1 and V2 can share the same auth: https://developer.twitter.com/en/docs/twitter-api/tweets/search/migrate/standard-to-twitter-api-v2
OAuth 1.0a User Context and OAuth 2.0 Bearer Token authentication
The v1.1 search/tweets and the Twitter API v2 recent search endpoint support both OAuth 1.0a User Context and OAuth 2.0 Bearer Token.
Therefore, if you were previously using the standard v1.1 search endpoint you can continue using the same authentication method if you migrate to the Twitter API v2 version.
First you have to add the app you’re using to a “Project” on the Developer dashboard. This step is necessary to be able to call v2 endpoints see: https://developer.twitter.com/en/docs/projects/overview
I got this answer from the twitter community forum and now it works fine with the code posted in my question above.

Twitter webhook registration error | {"errors":[{"code":32,"message":"Could not authenticate you."}]}

when I try to register the twitter web-hook, api returns the error message "Could not authenticate you".
I am using the below php wrapper for twitter api v1.1.
twitter-api-php
$settings = array(
'oauth_access_token' => $accesstoken,
'oauth_access_token_secret' => $accesstokensecret,
'consumer_key' => $consumerkey,
'consumer_secret' => $consumersecret
);
$url = 'https://api.twitter.com/1.1/account_activity/all/msgdev/webhooks.json?url=https://myapp.com/twitterwebhook.php';
$requestMethod = 'POST';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)->performRequest();
I am able to send message. But not able to create webhook.
Created developer account and got api access, also created twitter app in apps.twitter.com
You can fix this issue by doing a url-encode on your URL parameter. Refer the sample code on how we are doing it in ruby.
response = #twitter_api.post('https://api.twitter.com/1.1/account_activity/all/chatwootdev/webhooks.json?url=https%3A%2F%2Fchatwoot.com%2F16b66d68-c9f9-4c2e-8caa-4244cff8b483', nil, HEADERS)
Also note that you will need to implement a CRC check at your webhook url before you start getting the events from twitter. If the CRC is not implemented. you will be recieveing 214 error code.
ref: getting started with twitter accounts subscription api

How to Post to Facebook Page using Curl on SDK v4.1/5.00 2016 PHP API v2.5

I got my permanent token following this guide: http://php8legs.com/en/facebook/41-how-to-auto-publish-post-on-facebook-fan-page-as-admin-using-facebook-php-sdk-v4-graph-api-v2-2-2-get-permanent-page-access-token
I installed Facebook SDK using composer.
I then struggled to find a proper script. If i use something like this: (removed the variable and the required)
$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
'access_token' => $access_token,
'name' => 'Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x',
'link' => 'https://www.webniraj.com/2014/08/23/facebook-api-posting-as-a-page-using-graph-api-v2-x-and-php-sdk-4-0-x/',
'caption' => 'The Facebook API lets you post to Pages you administrate via the API. This tutorial shows you how to achieve this using the Facebook PHP SDK v4.0.x and Graph API 2.x.',
'message' => 'Check out my new blog post!',
) ))->execute()->getGraphObject()->asArray();
// return post_id
print_r( $page_post );
well then it doesn't work I get the following error:
Fatal error: Class 'FacebookRequest' not found in..."

Using Oauth with GuzzleHttp and Magento API in PHP

I'm trying to access Magento's REST API in PHP, using Oauth and GuzzleHttp.
I'm basing my example off of this Twitter example:
https://github.com/guzzle/oauth-subscriber
My code so far is:
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use GuzzleHttp\Client;
$client = new Client(array(
'base_url' => 'http://magento',
'defaults' => array('auth' => 'oauth')
));
$oauth = new Oauth1(array(
'consumer_key' => 'magento_key',
'consumer_secret' => 'magento_secret'
));
$client->getEmitter()->attach($oauth);
$response = $client->post('/oauth/initiate');
print_r($response);die();
My host is http://magento. I'm trying to POST my key and secret to /oauth/initiate like is says in the Magento docs.
http://www.magentocommerce.com/api/rest/authentication/oauth_authentication.html#OAuthAuthentication-AuthenticationEndpoints
Does anyone have any insight on what I might be doing wrong? I'm getting a 500 error using Firefox's RESTClient to make this call.
EDIT:
I'm now getting this exception thrown
{
"response": "Client error response [url] http://magento/oauth/initiate [status code] 400 [reason phrase] Bad Request"
}
It's getting a 400 from http://magento/oauth/initiate but this is only when using GuzzleHttp and Oauth. If I make the POST directly within RESTClient as http://magento/oauth/initiate, I get an error response from Magento itself (so it does not 400), and if I add all the necessary params I get the appropriate token responses.
UPDATE:
After inspecting the exception being thrown I'm seeing my authorization array does not conain my callback url. This is suppose to be http://magento/oauth/authorize which I've adde in the RESTClient and it works with the token response. Iv'e tried adding this in my $oauth array as 'callback' and 'oauth_callback' but neither seem to work. I can see this appears in the exception, under my data array, but not in the [authorize] array which I believe it needs to be. I tihnk that's what's missing, if someone has an idea of where it needs to be placed.

UserVoice's OAuth Implementation in PHP

I am able to get the Records of the UserVoice by calling and Unauthorized Requests but now i want to create and Update things in which i need OAuth but i am unable to find the UserVoice's PHP Implementation of OAuth and Create/Update/Delete with UserVoice API is there anyone can guide me how to Implement such thing??
We have quite recently worked on this and released an OAuth-based library which makes it a bit easier to access UserVoice API from a PHP version 5 application.
An example on how to create a ticket using an access token for user#example.com:
<?php
require_once('vendor/autoload.php'); // Using Composer
$client = new \UserVoice\Client('subdomain-name', 'API KEY', 'API SECRET');
$access_token = $client->login_as('user#example.com');
$result = $access_token->post("/api/v1/tickets", array(
'ticket' => array(
'state' => 'open',
'subject' => 'PHP SDK',
'message' => "Would you have any OAuth example for PHP?"
)
));
?>
Despite the fact that the library is still under development, I believe it's already useful to you. See the blog post and more examples like responding to tickets as an account owner at:
http://developer.uservoice.com/docs/api/php-sdk/

Categories