Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
joshine
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
joshine
Commits
48f199a1
Commit
48f199a1
authored
Apr 14, 2023
by
halweg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat : feed auth
parent
a907d785
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
129 additions
and
3 deletions
+129
-3
app/code/Joshine/InstagramFeed/Block/ApiAddress.php
+16
-1
app/code/Joshine/InstagramFeed/Controller/Adminhtml/OAuth/Redirect.php
+113
-2
No files found.
app/code/Joshine/InstagramFeed/Block/ApiAddress.php
View file @
48f199a1
...
...
@@ -11,7 +11,12 @@ use Magento\Store\Model\ScopeInterface;
class
ApiAddress
extends
Template
{
const
INSTAGRAM_API_BASE_URL
=
'https://api.instagram.com'
;
const
INSTAGRAM_AUTH_URL
=
'oauth/authorize'
;
const
INSTAGRAM_GRAPH_API_URL
=
"https://graph.instagram.com"
;
const
INSTAGRAM_AUTH_URL
=
'oauth/authorize'
;
const
INSTAGRAM_TOKEN_URL
=
'oauth/access_token'
;
const
REDIRECT_URL
=
'admin/joshine_instagram/oauth/redirect'
;
...
...
@@ -36,6 +41,16 @@ class ApiAddress extends Template
);
}
public
function
getLongTokenUrl
()
{
return
self
::
INSTAGRAM_GRAPH_API_URL
.
"/access_token"
;
}
public
function
getTokenUrl
()
:
string
{
return
self
::
INSTAGRAM_API_BASE_URL
.
'/'
.
self
::
INSTAGRAM_TOKEN_URL
;
}
public
function
getAppSecret
()
{
return
$this
->
_objectManager
->
get
(
ScopeConfigInterface
::
class
)
...
...
app/code/Joshine/InstagramFeed/Controller/Adminhtml/OAuth/Redirect.php
View file @
48f199a1
<?php
namespace
Joshine\InstagramFeed\Controller\Adminhtml\OAuth
;
use
Joshine\InstagramFeed\Block\ApiAddress
;
use
\Magento\Backend\App\Action
;
use
Magento\Backend\App\Action\Context
;
use
Magento\Framework\App\Config\ScopeConfigInterface
;
use
Magento\Framework\HTTP\Client\Curl
;
use
Magento\Store\Model\ScopeInterface
;
class
Redirect
extends
Action
{
protected
$_publicActions
=
[
'redirect'
];
protected
$_apiAddress
;
/**
* @var ApiAddress
*/
private
$_curlClient
;
/**
* @var \Magento\Framework\App\Config\Storage\WriterInterface
*/
private
$_configWriter
;
private
$_cacheTypeList
;
public
function
__construct
(
Context
$context
,
Curl
$curl
,
\Magento\Framework\App\Config\Storage\WriterInterface
$configWriter
,
ApiAddress
$apiAddress
,
\Magento\Framework\App\Cache\TypeListInterface
$cacheTypeList
)
{
$this
->
_curlClient
=
$curl
;
$this
->
_configWriter
=
$configWriter
;
$this
->
_apiAddress
=
$apiAddress
;
$this
->
_cacheTypeList
=
$cacheTypeList
;
parent
::
__construct
(
$context
);
}
public
function
getCurlClient
()
{
return
$this
->
_curlClient
;
}
public
function
execute
()
{
echo
"1111"
;
// TODO: Implement execute() method.
$codeRaw
=
$this
->
getRequest
()
->
getParam
(
'code'
);
if
(
!
$codeRaw
)
{
return
"Error Code"
;
}
$code
=
rtrim
(
$codeRaw
,
"#_"
);
try
{
$oauthRes
=
$this
->
getToken
(
$code
);
if
(
!
isset
(
$oauthRes
[
'access_token'
]))
{
return
;
}
$longTokenRaw
=
$this
->
getLongToken
(
$oauthRes
[
"access_token"
]);
$longToken
=
json_decode
(
$longTokenRaw
,
true
);
if
(
!
isset
(
$longToken
[
'access_token'
]))
{
echo
$longTokenRaw
;
return
;
}
$userId
=
$oauthRes
[
'user_id'
];
$longLifeToken
=
$oauthRes
[
'access_token'
];
$this
->
_configWriter
->
save
(
"joshine_instagram_feed/general/user_id"
,
$userId
);
$this
->
_configWriter
->
save
(
"joshine_instagram_feed/general/access_token"
,
$longLifeToken
);
$this
->
_cacheTypeList
->
cleanType
(
\Magento\Framework\App\Cache\Type\Config
::
TYPE_IDENTIFIER
);
echo
"Success!"
;
return
;
}
catch
(
\Exception
$e
)
{
\Magento\Framework\App\ObjectManager
::
getInstance
()
->
get
(
'Psr\Log\LoggerInterface'
)
->
warning
(
$e
->
getMessage
());
}
echo
"Something error, try again"
;
}
private
function
getLongToken
(
$token
)
{
$url
=
$this
->
_apiAddress
->
getLongTokenUrl
()
.
"?grant_type=ig_exchange_token"
.
"&client_secret=
{
$this
->
_apiAddress
->
getAppSecret
()
}
"
.
"&access_token=
{
$token
}
"
;
$this
->
getCurlClient
()
->
get
(
$url
);
$this
->
getCurlClient
()
->
setOptions
([
CURLOPT_SSL_VERIFYHOST
=>
false
,
CURLOPT_SSL_VERIFYPEER
=>
false
]);
return
$this
->
getCurlClient
()
->
getBody
();
}
private
function
getToken
(
$code
)
{
$request
=
[
'code'
=>
$code
,
'client_secret'
=>
$this
->
_apiAddress
->
getAppSecret
(),
'redirect_uri'
=>
$this
->
_apiAddress
->
redirectUri
(),
'grant_type'
=>
'authorization_code'
,
'client_id'
=>
$this
->
_apiAddress
->
getAppid
(),
];
$this
->
getCurlClient
()
->
post
(
$this
->
_apiAddress
->
getTokenUrl
(),
$request
);
$this
->
getCurlClient
()
->
setOptions
([
CURLOPT_SSL_VERIFYHOST
=>
false
,
CURLOPT_SSL_VERIFYPEER
=>
false
]);
if
(
$this
->
getCurlClient
()
->
getStatus
()
!=
200
)
{
ob_clean
();
echo
$this
->
getCurlClient
()
->
getBody
();
return
[];
}
return
json_decode
(
$this
->
getCurlClient
()
->
getBody
(),
true
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment