Making WordPress.org

Ticket #2907: 2907.diff

File 2907.diff, 5.9 KB (added by xkon, 8 years ago)

Get Access Token and parse personal account

  • classes/tggr-source-instagram.php

     
    2929                 */
    3030                protected function __construct() {
    3131                        $this->view_folder   = dirname( __DIR__ ) . '/views/'. str_replace( '.php', '', basename( __FILE__ ) );
    32                         $this->setting_names = array( 'Client ID', 'Highlighted Accounts', 'Banned Accounts', '_newest_media_id' );
     32                        $this->setting_names = array( 'Client ID', 'Client Secret', 'Redirect URL', 'Access Token', 'Highlighted Accounts', 'Banned Accounts', '_newest_media_id' );
    3333
    3434                        foreach ( $this->setting_names as $key ) {
    3535                                $this->default_settings[ strtolower( str_replace( ' ', '_', $key ) ) ] = '';
     
    138138                public function import_new_items( $hashtag ) {
    139139                        $media = self::get_new_media(
    140140                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['client_id'],
     141                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['access_token'],
    141142                                $hashtag,
    142143                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['_newest_media_id']
    143144                        );
     
    152153                 * @mvc Model
    153154                 *
    154155                 * @param string $client_id
     156                 * @param string $access_token
    155157                 * @param string $hashtag
    156158                 * @param string $max_id The ID of the most recent item that is already saved in the database
    157159                 * @return mixed string|false
    158160                 */
    159                 protected static function get_new_media( $client_id, $hashtag, $max_id ) {
     161                protected static function get_new_media( $client_id, $access_token, $hashtag, $max_id ) {
    160162                        $response = $media = false;
    161163
    162                         if ( $client_id && $hashtag ) {
    163                                 $url = sprintf(
    164                                         '%s/v1/tags/%s/media/recent?client_id=%s',
     164                        // url for SELF posts https://api.instagram.com/v1/users/self/media/recent/?access_token=XXXX
     165                        /*
     166                         * $url = sprintf(
     167                                        '%s/v1/users/self/media/recent?access_token=%s&count=6',
    165168                                        self::API_URL,
     169                                        urlencode( $access_token )
     170                                );
     171                         */
     172                        // url for PUBLIC tags // https://api.instagram.com/v1/tags/XXXX/media/recent/?access_token=XXXX
     173                        /*
     174                         * $url = sprintf(
     175                                        '%s/v1/tags/%s/media/recent?access_token=%s',
     176                                        self::API_URL,
    166177                                        urlencode( str_replace( '#', '', $hashtag ) ),
    167                                         urlencode( $client_id )
     178                                        urlencode( $access_token )
    168179                                );
     180                         */
    169181
     182                        if ( $access_token && $hashtag ) {
     183                                $url = sprintf(
     184                                        '%s/v1/users/self/media/recent?access_token=%s&count=6',
     185                                        self::API_URL,
     186                                        urlencode( $access_token )
     187                                );
     188
    170189                                $response = wp_remote_get( $url );
    171190                                $body     = json_decode( wp_remote_retrieve_body( $response ) );
    172191
     
    175194                                }
    176195                        }
    177196
    178                         self::log( __METHOD__, 'Results', compact( 'client_id', 'hashtag', 'max_id', 'response' ) );
     197//                      self::log( __METHOD__, 'Results', compact( 'access_token', 'hashtag', 'max_id', 'response' ) );
    179198
    180199                        return $media;
    181200                }
  • views/tggr-source-instagram/page-settings-section-header.php

     
    1 <p>You can obtain the Client ID by logging into <a href="https://www.instagram.com/developer/">Instagram's developer portal</a>, and then registering a new client.</p>
     1<p>Instructions:</p>
     2<p>1. You can obtain the Client ID &amp; Client Secret by logging into <a href="https://www.instagram.com/developer/">Instagram's developer portal</a>, and then registering a new client. Insert them to the fields bellow and click 'Save Changes'.</p>
     3<p></p>
     4<p>2. Copy the Redirect URL from the field below and paste it in your <strong>Valid redirect URIs</strong> field in your Instagram API Client Settings.</p>
     5<p></p>
     6<p>3. <a href="" id="get_access_token">Click here to get your Access Token!</a> - After the Access Token is in the field please press Save Changes.</p>
     7<p></p>
     8<?php
     9$tggroptions = get_option( 'tggr_settings', array() );
     10$cid = $tggroptions['TGGRSourceInstagram']['client_id'];
     11$cse = $tggroptions['TGGRSourceInstagram']['client_secret'];
     12$cre = $tggroptions['TGGRSourceInstagram']['redirect_url'];
     13$fat = $tggroptions['TGGRSourceInstagram']['access_token'];
     14
     15if ( ! empty( $_GET['code'] ) ) {
     16        $icode = sanitize_text_field( $_GET['code'] );
     17} else {
     18        $icode = '';
     19}
     20
     21if ( $icode !== '' && $fat === '' ) {
     22
     23        $uri = 'https://api.instagram.com/oauth/access_token';
     24        $data = [
     25                'client_id' => $cid,
     26                'client_secret' => $cse,
     27                'grant_type' => 'authorization_code',
     28                'redirect_uri' => $cre,
     29                'code' => $icode,
     30        ];
     31
     32        $ch = curl_init();
     33
     34        curl_setopt( $ch, CURLOPT_URL, $uri ); // uri
     35        curl_setopt( $ch, CURLOPT_POST, true ); // POST
     36        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); // POST DATA
     37        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // RETURN RESULT true
     38        curl_setopt( $ch, CURLOPT_HEADER, 0 ); // RETURN HEADER false
     39        curl_setopt( $ch, CURLOPT_NOBODY, 0 ); // NO RETURN BODY false / we need the body to return
     40        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 ); // VERIFY SSL HOST false
     41        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); // VERIFY SSL PEER false
     42
     43        $result = json_decode( curl_exec( $ch ), true ); // execute curl
     44
     45        $at = $result['access_token'];
     46
     47        if ( $at !== '' ) {
     48                ?>
     49                <script>
     50                        (function( $ ) {
     51                                $( document ).ready( function() {
     52                                        $( '#tggr_instagram_access_token' ).val( '<?php echo $at; ?>' );
     53                                });
     54                        })( jQuery );
     55                </script>
     56                <?php
     57        }
     58}
     59?>
     60<script>
     61(function( $ ) {
     62        $( document ).ready( function() {
     63                $( '#tggr_instagram_redirect_url' ).val(window.location.href);
     64                $( '#get_access_token' ).attr('href', 'https://www.instagram.com/oauth/authorize/?client_id=' + $('#tggr_instagram_client_id').val() + '&redirect_uri=' + $( '#tggr_instagram_redirect_url' ).val() + '&response_type=code');
     65        });
     66})( jQuery );
     67</script>
     68 No newline at end of file