· 7 years ago · Mar 05, 2018, 06:00 PM
1http://localhost:4200/?oauth_token=tokenOauth&oauth_verifier=verifiertoken
2
3<?php
4/**
5 * Example of products list retrieve using admin account via Magento REST
6API. oAuth authorization is used
7 */
8$callbackUrl = "http://localhost:4200"; // angular2 page redirect here
9$temporaryCredentialsRequestUrl = "http://magentohost.co.uk/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
10$adminAuthorizationUrl = 'http://magentohost.co.uk/admin/oauth_authorize';
11$accessTokenRequestUrl = 'http://magentohost.co.uk/oauth/token';
12$apiUrl = 'http://magentohost.co.uk/';
13$consumerKey = 'myconsuerkey';
14$consumerSecret = 'myconsuerkeysecret';
15session_start();
16if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) &&
17$_SESSION['state'] == 1) {
18 $_SESSION['state'] = 0;
19}
20try {
21 $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION
22: OAUTH_AUTH_TYPE_URI;
23 $oauthClient = new OAuth($consumerKey, $consumerSecret,
24OAUTH_SIG_METHOD_HMACSHA1, $authType);
25 $oauthClient->enableDebug();
26 if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
27 $requestToken =
28$oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
29 $_SESSION['secret'] = $requestToken['oauth_token_secret'];
30 $_SESSION['state'] = 1;
31 header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' .
32$requestToken['oauth_token']);
33 exit;
34 } else if ($_SESSION['state'] == 1) {
35 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
36 $accessToken =
37$oauthClient->getAccessToken($accessTokenRequestUrl);
38 $_SESSION['state'] = 2;
39 $_SESSION['token'] = $accessToken['oauth_token'];
40 $_SESSION['secret'] = $accessToken['oauth_token_secret'];
41 header('Location: ' . $callbackUrl);
42 exit;
43 } else {
44 $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
45 $resourceUrl = "$apiUrl/products";
46 $oauthClient->fetch($resourceUrl);
47 $productsList = json_decode($oauthClient->getLastResponse());
48 print_r($productsList);
49 }
50} catch (OAuthException $e) {
51 print_r($e);
52}
53
54searchItem(query) {
55 let headers = new Headers({ 'Accept': 'application/json' });
56 headers.set("Authorization","Basic 8548545151521438");
57
58 return this.htt.get('http://magentohost.co.uk/api/rest/products', {headers: headers})
59 .map(res => res.json());
60 }