source: extensions/oAuth/include/hybridauth/Hybrid/Providers/Tumblr.php @ 20293

Last change on this file since 20293 was 20293, checked in by mistic100, 11 years ago

first commit of oAuth plugin, still in developpement

File size: 2.6 KB
Line 
1<?php
2/*!
3* HybridAuth
4* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
5* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
6*/
7
8/**
9* Hybrid_Providers_Tumblr
10*/
11class Hybrid_Providers_Tumblr extends Hybrid_Provider_Model_OAuth1
12{
13        /**
14        * IDp wrappers initializer
15        */
16        function initialize()
17        {
18                parent::initialize();
19
20                // provider api end-points
21                $this->api->api_base_url      = "http://api.tumblr.com/v2/";
22                $this->api->authorize_url     = "http://www.tumblr.com/oauth/authorize";
23                $this->api->request_token_url = "http://www.tumblr.com/oauth/request_token";
24                $this->api->access_token_url  = "http://www.tumblr.com/oauth/access_token";
25
26                $this->api->curl_auth_header  = false;
27        }
28
29
30   /**
31        * load the user profile from the IDp api client
32        */
33        function getUserProfile()
34        { 
35                $response = $this->api->get( 'http://www.tumblr.com/api/authenticate' );
36
37                // check the last HTTP status code returned
38                if ( $this->api->http_code != 200 )
39                {
40                        throw new Exception( "User profile request failed! {$this->providerId} returned an error: " . $this->errorMessageByStatus( $this->api->http_code ), 6 );
41                }
42
43                try{ 
44                        $profile = $this->api->get( 'user/info' );
45
46                        foreach ( $profile->response->user->blogs as $blog ) {
47                                if( $blog->primary ){
48                                        $bloghostname = explode( '://', $blog->url );
49                                        $bloghostname = substr( $bloghostname[1], 0, -1);
50
51                                        // store the user primary blog base hostname
52                                        $this->token( "primary_blog" , $bloghostname );
53
54                                        $this->user->profile->identifier        = $blog->url;
55                                        $this->user->profile->displayName       = $profile->response->user->name;
56                                        $this->user->profile->profileURL        = $blog->url;
57                                        $this->user->profile->webSiteURL        = $blog->url;
58
59                                        $avatar = $this->api->get( 'blog/'. $this->token( "primary_blog" ) .'/avatar' );
60
61                                        $this->user->profile->photoURL          = $avatar->response->avatar_url;
62
63                                        break; 
64                                }
65                        } 
66                }
67                catch( Exception $e ){
68                        throw new Exception( "User profile request failed! {$this->providerId} returned an error while requesting the user profile.", 6 );
69                }       
70       
71                return $this->user->profile;
72        }
73
74        /**
75        * post to tumblr
76        */ 
77        function setUserStatus( $status )
78        {
79                $parameters = array( 'type' => "text", 'body' => $status ); 
80                $response  = $this->api->post( "blog/" . $this->token( "primary_blog" ) . '/post', $parameters ); 
81
82                if ( $response->meta->status != 201 ){
83                        throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $response->meta->status ) );
84                } 
85        }
86}
Note: See TracBrowser for help on using the repository browser.