Skip to content

Instantly share code, notes, and snippets.

@gmjelle
Forked from younes0/DiscogsApi.md
Last active September 20, 2015 16:42
Show Gist options
  • Save gmjelle/56cf57068ece4a470a06 to your computer and use it in GitHub Desktop.
Save gmjelle/56cf57068ece4a470a06 to your computer and use it in GitHub Desktop.
Setting up Discogs PHP API client with OAuth feature

Setting up Discogs PHP API client with OAuth feature

Get these composer packages

"guzzlehttp/guzzle": "4.1.x",
"guzzlehttp/oauth-subscriber": "0.1.x",
"league/oauth1-client": "~1.0",

Auto-load this class

<?php

namespace League\OAuth1\Client\Server;

use League\OAuth1\Client\Credentials\TokenCredentials;
use Guzzle\Service\Client as GuzzleClient;

class Discogs extends Server
{

	protected $userAgent;

	public function __construct($clientCredentials, SignatureInterface $signature = null, $userAgent = null)
	{
		$this->userAgent = $userAgent;
		parent::__construct($clientCredentials, $signature);
	}

	public function createHttpClient()
	{
		$client = new GuzzleClient();
		$this->userAgent and $client->setUserAgent($this->userAgent);

		return $client;
	}

	public function urlTemporaryCredentials()
	{
		return 'http://api.discogs.com/oauth/request_token';
	}

	public function urlAuthorization()
	{
		return 'http://discogs.com/oauth/authorize';
	}

	public function urlTokenCredentials()
	{
		return 'http://api.discogs.com/oauth/access_token';
	}

	public function userDetails($data, TokenCredentials $tokenCredentials) {}
	public function userUid($data, TokenCredentials $tokenCredentials) {}
	public function userEmail($data, TokenCredentials $tokenCredentials) {}
	public function userScreenName($data, TokenCredentials $tokenCredentials) {}
	public function urlUserDetails() {}

}

Now setup the oauth redirect feature, the api client and make calls

<?php 

class MyController 
{
	
	public function getIndex()
	{
		$userAgent = 'MyApp/1.0';  // specify recognizable user-agent

		$server = new \League\OAuth1\Client\Server\Discogs([
			'identifier'   => DISCOGS_KEY,
			'secret'       => DISCOGS_SECRET,
			'callback_uri' => 'http://mysite.com/'
		], null, $userAgent);

		// no temporary token? redirect then
		if ( !isset($_GET['oauth_token']) ) {
			$tempCredentials = $server->getTemporaryCredentials();
			$_SESSION['tempCredentials'] = serialize($tempCredentials);
			header('Location: '.$server->getAuthorizationUrl($tempCredentials));
		}

		// ok got temporary token
		// nb: you may save it in db
		$token = $server->getTokenCredentials(
			unserialize($_SESSION['tempCredentials']), 
			$_GET['oauth_token'],
			$_GET['oauth_verifier']
		);

		// build guzzle client to make requests
		$client = new GuzzleHttp\Client([
			'base_url' => 'http://api.discogs.com',
			'defaults' => [
				'headers' => ['User-Agent' => $userAgent ],
				'auth'    => 'oauth',
			],
		]);

		// attach oauth subscriber to grab images
		$client->getEmitter()->attach(new GuzzleHttp\Subscriber\Oauth\Oauth1([
			'consumer_key'    => DISCOGS_KEY,
			'consumer_secret' => DISCOGS_SECRET,
			'token'           => $token->getIdentifier(),
			'token_secret'    => $token->getSecret(),
		]));

		// make calls
		$results = $client->get('/database/search', [ 'query' => [
			'q'    => 'Justin Bieber',
			'type' => 'artist',
		]])->json();

		// get image body
		$rawImage = $client
			->get('http://path-to-restricted-discogs-cover')
			->getBody();	
	}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment