Recommend this page to a friend! |
PHP OAuth Library | > | PHP OAuth Library package blog | > | How to Implement PHP ... | > | All threads | > | Oauth connection | > | (Un) Subscribe thread alerts |
|
![]() it was not possible to access the OAuth refresh token: it was returned an unexpected response status 400 Response: {"error":"invalid_grant","error_description":"Unknown or previously used refresh_token}
OAUTH_STATE|s:17:"1420608408-b7f88f";OAUTH_ACCESS_TOKEN|a:1:{s:39:"https://secure.meetup.com/oauth2/access";a:5:{s:5:"value";s:32:"cc64fa9abdb70664725d4cae0aecd64c";s:10:"authorized";b:1;s:6:"expiry";s:19:"2015-01-07 06:33:39";s:4:"type";s:6:"bearer";s:7:"refresh";s:32:"80283fd74e0539ec58105879b457fdc9";}} its value stored xampp\tmp folder ..what is mistake is there?
![]() here attach code:
login_with_meetup.php: <?php /* * login_with_meetup.php * * @(#) $Id: login_with_meetup.php,v 1.1 2014/12/23 03:51:59 mlemos Exp $ * */ /* * Get the http.php file from http://www.phpclasses.org/httpclient */ require('http.php'); require('oauth_client.php'); $client = new oauth_client_class; $client->debug = false; $client->debug_http = true; $client->server = 'Meetup'; $client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST']. dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login_with_meetup.php'; $client->client_id = 'fp78026pqda5kvipnov9gvh9ro'; $application_line = __LINE__; $client->client_secret = 'j58snihdtm9hcs9ml2br80cn26'; if(strlen($client->client_id) == 0 || strlen($client->client_secret) == 0) die('Please go to your Meetup OAuth Consumers page '. 'https://secure.meetup.com/meetup_api/oauth_consumers/ , '. 'create a consumer, and in the line '.$application_line. ' set the client_id to Key and client_secret with Secret. '. 'The Callback URL must be '.$client->redirect_uri. ' Make sure the Website URL is not in a private network '. 'and is accessible to the Meetup site.'); if(($success = $client->Initialize())) { if(($success = $client->Process())) { if(strlen($client->access_token)) { $success = $client->CallAPI( 'https://api.meetup.com/2/events?&sign=true&photo-host=public&member_id=self&page=20', 'GET', array(), array('FailOnAccessError'=>true), $user); } } $success = $client->Finalize($success); } if($client->exit) exit; if($success) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Meetup OAuth client results</title> </head> <body> <?php echo '<h1>', ' you have logged in successfully with Meetup!</h1>'; echo '<pre>', HtmlSpecialChars(print_r($user, 1)), '</pre>'; ?> </body> </html> <?php } else { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>OAuth client error</title> </head> <body> <h1>OAuth client error</h1> <p>Error: <?php echo HtmlSpecialChars($client->error); ?></p> </body> </html> <?php } ?> oauth_client.php: <?php /* * oauth_client.php * * @(#) $Id: oauth_client.php,v 1.125 2014/12/23 03:51:59 mlemos Exp $ * */ /* {metadocument}<?xml version="1.0" encoding="ISO-8859-1" ?> <class> <package>net.manuellemos.oauth</package> <version>@(#) $Id: oauth_client.php,v 1.125 2014/12/23 03:51:59 mlemos Exp $</version> <copyright>Copyright © (C) Manuel Lemos 2012</copyright> <title>OAuth client</title> <author>Manuel Lemos</author> <authoraddress>mlemos-at-acm.org</authoraddress> <documentation> <idiom>en</idiom> <purpose>This class serves two main purposes:<paragraphbreak /> 1) Implement the OAuth protocol to retrieve a token from a server to authorize the access to an API on behalf of the current user.<paragraphbreak /> 2) Perform calls to a Web services API using a token previously obtained using this class or a token provided some other way by the Web services provider.</purpose> <usage>Regardless of your purposes, you always need to start calling the class <functionlink>Initialize</functionlink> function after initializing setup variables. After you are done with the class, always call the <functionlink>Finalize</functionlink> function at the end.<paragraphbreak /> This class supports either OAuth protocol versions 1.0, 1.0a and 2.0. It abstracts the differences between these protocol versions, so the class usage is the same independently of the OAuth version of the server.<paragraphbreak /> The class also provides built-in support to several popular OAuth servers, so you do not have to manually configure all the details to access those servers. Just set the <variablelink>server</variablelink> variable to configure the class to access one of the built-in supported servers.<paragraphbreak /> If you need to access one type of server that is not yet directly supported by the class, you need to configure it explicitly setting the variables: <variablelink>oauth_version</variablelink>, <variablelink>url_parameters</variablelink>, <variablelink>authorization_header</variablelink>, <variablelink>request_token_url</variablelink>, <variablelink>dialog_url</variablelink>, <variablelink>pin_dialog_url</variablelink>, <variablelink>offline_dialog_url</variablelink>, <variablelink>append_state_to_redirect_uri</variablelink> and <variablelink>access_token_url</variablelink>.<paragraphbreak /> Before proceeding to the actual OAuth authorization process, you need to have registered your application with the OAuth server. The registration provides you values to set the variables <variablelink>client_id</variablelink> and <variablelink>client_secret</variablelink>. Some servers also provide an additional value to set the <variablelink>api_key</variablelink> variable.<paragraphbreak /> You also need to set the variables <variablelink>redirect_uri</variablelink> and <variablelink>scope</variablelink> before calling the <functionlink>Process</functionlink> function to make the class perform the necessary interactions with the OAuth server.<paragraphbreak /> The OAuth protocol involves multiple steps that include redirection to the OAuth server. There it asks permission to the current user to grant your application access to APIs on his/her behalf. When there is a redirection, the class will set the <variablelink>exit</variablelink> variable to <booleanvalue>1</booleanvalue>. Then your script should exit immediately without outputting anything.<paragraphbreak /> When the OAuth access token is successfully obtained, the following variables are set by the class with the obtained values: <variablelink>access_token</variablelink>, <variablelink>access_token_secret</variablelink>, <variablelink>access_token_expiry</variablelink>, <variablelink>access_token_type</variablelink>. You may want to store these values to use them later when calling the server APIs.<paragraphbreak /> If there was a problem during OAuth authorization process, check the variable <variablelink>authorization_error</variablelink> to determine the reason.<paragraphbreak /> Once you get the access token, you can call the server APIs using the <functionlink>CallAPI</functionlink> function. Check the <variablelink>access_token_error</variablelink> variable to determine if there was an error when trying to to call the API.<paragraphbreak /> If for some reason the user has revoked the access to your application, you need to ask the user to authorize your application again. First you may need to call the function <functionlink>ResetAccessToken</functionlink> to reset the value of the access token that may be cached in session variables.</usage> </documentation> {/metadocument} */ class oauth_client_class { /* {metadocument} <variable> <name>error</name> <type>STRING</type> <value></value> <documentation> <purpose>Store the message that is returned when an error occurs.</purpose> <usage>Check this variable to understand what happened when a call to any of the class functions has failed.<paragraphbreak /> This class uses cumulative error handling. This means that if one class functions that may fail is called and this variable was already set to an error message due to a failure in a previous call to the same or other function, the function will also fail and does not do anything.<paragraphbreak /> This allows programs using this class to safely call several functions that may fail and only check the failure condition after the last function call.<paragraphbreak /> Just set this variable to an empty string to clear the error condition.</usage> </documentation> </variable> {/metadocument} */ var $error = ''; /* {metadocument} <variable> <name>debug</name> <type>BOOLEAN</type> <value>0</value> <documentation> <purpose>Control whether debug output is enabled</purpose> <usage>Set this variable to <booleanvalue>1</booleanvalue> if you need to check what is going on during calls to the class. When enabled, the debug output goes either to the variable <variablelink>debug_output</variablelink> and the PHP error log.</usage> </documentation> </variable> {/metadocument} */ var $debug = false; /* {metadocument} <variable> <name>debug_http</name> <type>BOOLEAN</type> <value>0</value> <documentation> <purpose>Control whether the dialog with the remote Web server should also be logged.</purpose> <usage>Set this variable to <booleanvalue>1</booleanvalue> if you want to inspect the data exchange with the OAuth server.</usage> </documentation> </variable> {/metadocument} */ var $debug_http = false; /* {metadocument} <variable> <name>exit</name> <type>BOOLEAN</type> <value>0</value> <documentation> <purpose>Determine if the current script should be exited.</purpose> <usage>Check this variable after calling the <functionlink>Process</functionlink> function and exit your script immediately if the variable is set to <booleanvalue>1</booleanvalue>.</usage> </documentation> </variable> {/metadocument} */ var $exit = false; /* {metadocument} <variable> <name>debug_output</name> <type>STRING</type> <value></value> <documentation> <purpose>Capture the debug output generated by the class</purpose> <usage>Inspect this variable if you need to see what happened during the class function calls.</usage> </documentation> </variable> {/metadocument} */ var $debug_output = ''; /* {metadocument} <variable> <name>debug_prefix</name> <type>STRING</type> <value>OAuth client: </value> <documentation> <purpose>Mark the lines of the debug output to identify actions performed by this class.</purpose> <usage>Change this variable if you prefer the debug output lines to be prefixed with a different text.</usage> </documentation> </variable> {/metadocument} */ var $debug_prefix = 'OAuth client: '; /* {metadocument} <variable> <name>server</name> <type>STRING</type> <value></value> <documentation> <purpose>Identify the type of OAuth server to access.</purpose> <usage>The class provides built-in support to several types of OAuth servers. This means that the class can automatically initialize several configuration variables just by setting this server variable.<paragraphbreak /> Currently it supports the following servers: <stringvalue>37Signals</stringvalue>, <stringvalue>Amazon</stringvalue>, <stringvalue>Bitbucket</stringvalue>, <stringvalue>Bitly</stringvalue>, <stringvalue>Box</stringvalue>, <stringvalue>Buffer</stringvalue>, <stringvalue>Copy</stringvalue>, <stringvalue>Dailymotion</stringvalue>, <stringvalue>Discogs</stringvalue>, <stringvalue>Disqus</stringvalue>, <stringvalue>Dropbox</stringvalue> (Dropbox with OAuth 1.0), <stringvalue>Dropbox2</stringvalue> (Dropbox with OAuth 2.0), <stringvalue>Etsy</stringvalue>, <stringvalue>Eventful</stringvalue>, <stringvalue>Facebook</stringvalue>, <stringvalue>Fitbit</stringvalue>, <stringvalue>Flickr</stringvalue>, <stringvalue>Foursquare</stringvalue>, <stringvalue>github</stringvalue>, <stringvalue>Google</stringvalue>, <stringvalue>Google1</stringvalue> (Google with OAuth 1.0), <stringvalue>imgur</stringvalue>, <stringvalue>Intuit</stringvalue>, <stringvalue>Instagram</stringvalue>, <stringvalue>Jawbone</stringvalue>, <stringvalue>LinkedIn</stringvalue>, <stringvalue>LinkedIn2</stringvalue> (LinkedIn with OAuth 2.0), <stringvalue>mail.ru</stringvalue>, <stringvalue>MailChimp</stringvalue>, <stringvalue>Mavenlink</stringvalue>, <stringvalue>Meetup</stringvalue>, <stringvalue>Microsoft</stringvalue>, <stringvalue>oDesk</stringvalue>, <stringvalue>Paypal</stringvalue>, <stringvalue>PaypalApplication</stringvalue>, <stringvalue>Rdio</stringvalue>, <stringvalue>Reddit</stringvalue>, <stringvalue>RunKeeper</stringvalue>, <stringvalue>Salesforce</stringvalue>, <stringvalue>Scoop.it</stringvalue>, <stringvalue>StockTwits</stringvalue>, <stringvalue>SurveyMonkey</stringvalue>, <stringvalue>TeamViewer</stringvalue>, <stringvalue>Tumblr</stringvalue>, <stringvalue>Twitter</stringvalue>, <stringvalue>Vimeo</stringvalue>, <stringvalue>VK</stringvalue>, <stringvalue>Withings</stringvalue>, <stringvalue>Wordpress</stringvalue>, <stringvalue>Xero</stringvalue>, <stringvalue>XING</stringvalue>, <stringvalue>Yahoo</stringvalue> and <stringvalue>Yandex</stringvalue>. Please contact the author if you would like to ask to add built-in support for other types of OAuth servers.<paragraphbreak /> If you want to access other types of OAuth servers that are not yet supported, set this variable to an empty string and configure other variables with values specific to those servers.</usage> </documentation> </variable> {/metadocument} */ var $server = ''; /* {metadocument} <variable> <name>configuration_file</name> <type>STRING</type> <value>oauth_configuration.json</value> <documentation> <purpose>Specify the path of the configuration file that defines the properties of additional OAuth server types.</purpose> <usage>Change the path in this variable if you are accessing a type of server without support built-in the class and you need to put the configuration file path in a different directory.</usage> </documentation> </variable> {/metadocument} */ var $configuration_file = 'oauth_configuration.json'; /* {metadocument} <variable> <name>request_token_url</name> <type>STRING</type> <value></value> <documentation> <purpose>URL of the OAuth server to request the initial token for OAuth 1.0 and 1.0a servers.</purpose> <usage>Set this variable to the OAuth request token URL when you are not accessing one of the built-in supported OAuth servers.<paragraphbreak /> For OAuth 1.0 and 1.0a servers, the request token URL can have certain marks that will act as template placeholders which will be replaced with given values before requesting the authorization token. Currently it supports the following placeholder marks:<paragraphbreak /> {SCOPE} - scope of the requested permissions to the granted by the OAuth server with the user permissions</usage> </documentation> </variable> {/metadocument} */ var $request_token_url = ''; /* {metadocument} <variable> <name>dialog_url</name> <type>STRING</type> <value></value> <documentation> <purpose>URL of the OAuth server to redirect the browser so the user can grant access to your application.</purpose> <usage>Set this variable to the OAuth request token URL when you are not accessing one of the built-in supported OAuth servers.<paragraphbreak /> For OAuth 1.0a servers that return the login dialog URL automatically, set this variable to <stringvalue>automatic</stringvalue><paragraphbreak /> For certain servers, the dialog URL can have certain marks that will act as template placeholders which will be replaced with values defined before redirecting the users browser. Currently it supports the following placeholder marks:<paragraphbreak /> {REDIRECT_URI} - URL to redirect when returning from the OAuth server authorization page<paragraphbreak /> {CLIENT_ID} - client application identifier registered at the server<paragraphbreak /> {SCOPE} - scope of the requested permissions to the granted by the OAuth server with the user permissions<paragraphbreak /> {STATE} - identifier of the OAuth session state<paragraphbreak /> {API_KEY} - API key to access the server</usage> </documentation> </variable> {/metadocument} */ var $dialog_url = ''; /* {metadocument} <variable> <name>pin_dialog_url</name> <type>STRING</type> <value></value> <documentation> <purpose>URL of the OAuth server to redirect the browser so the user can grant access to your application.</purpose> <usage>Set this variable when using the pin based authorization and the format of the of the authorization dialog page URL is different than the one set to the <variablelink>dialog_url</variablelink> variable.</usage> </documentation> </variable> {/metadocument} */ var $pin_dialog_url = ''; /* {metadocument} <variable> <name>offline_dialog_url</name> <type>STRING</type> <value></value> <documentation> <purpose>URL of the OAuth server to redirect the browser so the user can grant access to your application when offline access is requested.</purpose> <usage>Set this variable to the OAuth request token URL when you are not accessing one of the built-in supported OAuth servers and the OAuth server supports offline access.<paragraphbreak /> It should have the same format as the <variablelink>dialog_url</variablelink> variable.</usage> </documentation> </variable> {/metadocument} */ var $offline_dialog_url = ''; /* {metadocument} <variable> <name>pin</name> <type>STRING</type> <value></value> <documentation> <purpose>Value of the pin code for pin based authorization.</purpose> <usage>Set this value to the pin informed by the user when implementing the pin based authorization.<paragraphbreak /> Make sure the <variablelink>redirect_uri</variablelink> variable is set to <stringvalue>oob</stringvalue>.</usage> </documentation> </variable> {/metadocument} */ var $pin = ''; /* {metadocument} <variable> <name>append_state_to_redirect_uri</name> <type>STRING</type> <value></value> <documentation> <purpose>Pass the OAuth session state in a variable with a different name to work around implementation bugs of certain OAuth servers</purpose> <usage>Set this variable when you are not accessing one of the built-in supported OAuth servers if the OAuth server has a bug that makes it not pass back the OAuth state identifier in a request variable named state.</usage> </documentation> </variable> {/metadocument} */ var $append_state_to_redirect_uri = ''; /* {metadocument} <variable> <name>access_token_url</name> <type>STRING</type> <value></value> <documentation> <purpose>OAuth server URL that will return the access token URL.</purpose> <usage>Set this variable to the OAuth access token URL when you are not accessing one of the built-in supported OAuth servers.</usage> </documentation> </variable> {/metadocument} */ var $access_token_url = ''; /* {metadocument} <variable> <name>oauth_version</name> <type>STRING</type> <value>2.0</value> <documentation> <purpose>Version of the protocol version supported by the OAuth server.</purpose> <usage>Set this variable to the OAuth server protocol version when you are not accessing one of the built-in supported OAuth servers.</usage> </documentation> </variable> {/metadocument} */ var $oauth_version = '2.0'; /* {metadocument} <variable> <name>url_parameters</name> <type>BOOLEAN</type> <value>0</value> <documentation> <purpose>Determine if the API call parameters should be moved to the call URL.</purpose> <usage>Set this variable to <booleanvalue>1</booleanvalue> if the API you need to call requires that the call parameters always be passed via the API URL.</usage> </documentation> </variable> {/metadocument} */ var $url_parameters = false; /* {metadocument} <variable> <name>authorization_header</name> <type>BOOLEAN</type> <value>1</value> <documentation> <purpose>Determine if the OAuth parameters should be passed via HTTP Authorization request header.</purpose> <usage>Set this variable to <booleanvalue>1</booleanvalue> if the OAuth server requires that the OAuth parameters be passed using the HTTP Authorization instead of the request URI parameters.</usage> </documentation> </variable> {/metadocument} */ var $authorization_header = true; /* {metadocument} <variable> <name>token_request_method</name> <type>STRING</type> <value>GET</value> <documentation> <purpose>Define the HTTP method that should be used to request tokens from the server.</purpose> <usage>Set this variable to <stringvalue>POST</stringvalue> if the OAuth server does not support requesting tokens using the HTTP GET method.</usage> </documentation> </variable> {/metadocument} */ var $token_request_method = 'GET'; /* {metadocument} <variable> <name>signature_method</name> <type>STRING</type> <value>HMAC-SHA1</value> <documentation> <purpose>Define the method to generate the signature for API request parameters values.</purpose> <usage>Currently it supports <stringvalue>PLAINTEXT</stringvalue> and <stringvalue>HMAC-SHA1</stringvalue>.</usage> </documentation> </variable> {/metadocument} */ var $signature_method = 'HMAC-SHA1'; /* {metadocument} <variable> <name>redirect_uri</name> <type>STRING</type> <value></value> <documentation> <purpose>URL of the current script page that is calling this class</purpose> <usage>Set this variable to the current script page URL before proceeding the the OAuth authorization process.<paragraphbreak /> For pin based authorization, set this variable to <stringvalue>oob</stringvalue>.</usage> </documentation> </variable> {/metadocument} */ var $redirect_uri = 'http://localhost/xxx/login_with_meetup.php'; /* {metadocument} <variable> <name>client_id</name> <type>STRING</type> <value></value> <documentation> <purpose>Identifier of your application registered with the OAuth server</purpose> <usage>Set this variable to the application identifier that is provided by the OAuth server when you register the application.</usage> </documentation> </variable> {/metadocument} */ var $client_id = ''; /* {metadocument} <variable> <name>client_secret</name> <type>STRING</type> <value></value> <documentation> <purpose>Secret value assigned to your application when it is registered with the OAuth server.</purpose> <usage>Set this variable to the application secret that is provided by the OAuth server when you register the application.</usage> </documentation> </variable> {/metadocument} */ var $client_secret = ''; /* {metadocument} <variable> <name>api_key</name> <type>STRING</type> <value></value> <documentation> <purpose>Identifier of your API key provided by the OAuth server</purpose> <usage>Set this variable to the API key if the OAuth server requires one.</usage> </documentation> </variable> {/metadocument} */ var $api_key = ''; /* {metadocument} <variable> <name>get_token_with_api_key</name> <type>BOOLEAN</type> <value>0</value> <documentation> <purpose>Option to determine if the access token should be retrieved using the API key value instead of the client secret.</purpose> <usage>Set this variable to <booleanvalue>1</booleanvalue> if the OAuth server requires that the client secret be set to the API key when retrieving the OAuth token.</usage> </documentation> </variable> {/metadocument} */ var $get_token_with_api_key = false; /* {metadocument} <variable> <name>scope</name> <type>STRING</type> <value></value> <documentation> <purpose>Permissions that your application needs to call the OAuth server APIs</purpose> <usage>Check the documentation of the APIs that your application needs to call to set this variable with the identifiers of the permissions that the user needs to grant to your application.</usage> </documentation> </variable> {/metadocument} */ var $scope = ''; /* {metadocument} <variable> <name>offline</name> <type>BOOLEAN</type> <value>0</value> <documentation> <purpose>Specify whether it will be necessary to call the API when the user is not present and the server supports renewing expired access tokens using refresh tokens.</purpose> <usage>Set this variable to <booleanvalue>1</booleanvalue> if the server supports renewing expired tokens automatically when the user is not present.</usage> </documentation> </variable> {/metadocument} */ var $offline = false; /* {metadocument} <variable> <name>access_token</name> <type>STRING</type> <value></value> <documentation> <purpose>Access token obtained from the OAuth server</purpose> <usage>Check this variable to get the obtained access token upon successful OAuth authorization.</usage> </documentation> </variable> {/metadocument} */ var $access_token = ''; /* {metadocument} <variable> <name>access_token_secret</name> <type>STRING</type> <value></value> <documentation> <purpose>Access token secret obtained from the OAuth server</purpose> <usage>If the OAuth protocol version is 1.0 or 1.0a, check this variable to get the obtained access token secret upon successful OAuth authorization.</usage> </documentation> </variable> {/metadocument} */ var $access_token_secret = ''; /* {metadocument} <variable> <name>access_token_expiry</name> <type>STRING</type> <value></value> <documentation> <purpose>Timestamp of the expiry of the access token obtained from the OAuth server.</purpose> <usage>Check this variable to get the obtained access token expiry time upon successful OAuth authorization. If this variable is empty, that means no expiry time was set.</usage> </documentation> </variable> {/metadocument} */ var $access_token_expiry = ''; /* {metadocument} <variable> <name>access_token_type</name> <type>STRING</type> <value></value> <documentation> <purpose>Type of access token obtained from the OAuth server.</purpose> <usage>Check this variable to get the obtained access token type upon successful OAuth authorization.</usage> </documentation> </variable> {/metadocument} */ var $access_token_type = ''; /* {metadocument} <variable> <name>default_access_token_type</name> <type>STRING</type> <value></value> <documentation> <purpose>Type of access token to be assumed when the OAuth server does not specify an access token type.</purpose> <usage>Set this variable if the server requires a certain type of access token to be used but it does not specify a token type when the access token is returned.</usage> </documentation> </variable> {/metadocument} */ var $default_access_token_type = ''; /* {metadocument} <variable> <name>access_token_parameter</name> <type>STRING</type> <value></value> <documentation> <purpose>Name of the access token parameter to be passed in API call requests.</purpose> <usage>Set this variable to a non-empty string to override the default name for the access token parameter which is <stringvalue>oauth_token</stringvalue> of OAuth 1 and <stringvalue>access_token</stringvalue> for OAuth 2.</usage> </documentation> </variable> {/metadocument} */ var $access_token_parameter = ''; /* {metadocument} <variable> <name>access_token_response</name> <type>ARRAY</type> <documentation> <purpose>The original response for the access token request</purpose> <usage>Check this variable if the OAuth server returns custom parameters in the request to obtain the access token.</usage> </documentation> </variable> {/metadocument} */ var $access_token_response; /* {metadocument} <variable> <name>store_access_token_response</name> <type>BOOLEAN</type> <value>0</value> <documentation> <purpose>Option to determine if the original response for the access token request should be stored in the <variablelink>access_token_response</variablelink> variable.</purpose> <usage>Set this variable to <booleanvalue>1</booleanvalue> if the OAuth server returns custom parameters in the request to obtain the access token that may be needed in subsequent API calls.</usage> </documentation> </variable> {/metadocument} */ var $store_access_token_response = false; /* {metadocument} <variable> <name>access_token_authentication</name> <type>STRING</type> <value></value> <documentation> <purpose>Option to determine if the requests to obtain a new access token should use authentication to pass the application client ID and secret.</purpose> <usage>Set this variable to <stringvalue>basic</stringvalue> if the OAuth server requires that the the client ID and secret be passed using HTTP basic authentication headers when retrieving a new token.</usage> </documentation> </variable> {/metadocument} */ var $access_token_authentication = ''; /* {metadocument} <variable> <name>refresh_token</name> <type>STRING</type> <value></value> <documentation> <purpose>Refresh token obtained from the OAuth server</purpose> <usage>Check this variable to get the obtained refresh token upon successful OAuth authorization.</usage> </documentation> </variable> {/metadocument} */ var $refresh_token = ''; /* {metadocument} <variable> <name>access_token_error</name> <type>STRING</type> <value></value> <documentation> <purpose>Error message returned when a call to the API fails.</purpose> <usage>Check this variable to determine if there was an error while calling the Web services API when using the <functionlink>CallAPI</functionlink> function.</usage> </documentation> </variable> {/metadocument} */ var $access_token_error = ''; /* {metadocument} <variable> <name>authorization_error</name> <type>STRING</type> <value></value> <documentation> <purpose>Error message returned when it was not possible to obtain an OAuth access token</purpose> <usage>Check this variable to determine if there was an error while trying to obtain the OAuth access token.</usage> </documentation> </variable> {/metadocument} */ var $authorization_error = ''; /* {metadocument} <variable> <name>response_status</name> <type>INTEGER</type> <value>0</value> <documentation> <purpose>HTTP response status returned by the server when calling an API</purpose> <usage>Check this variable after calling the <functionlink>CallAPI</functionlink> function if the API calls and you need to process the error depending the response status. <integervalue>200</integervalue> means no error. <integervalue>0</integervalue> means the server response was not retrieved.</usage> </documentation> </variable> {/metadocument} */ var $response_status = 0; /* {metadocument} <variable> <name>oauth_username</name> <type>STRING</type> <value></value> <documentation> <purpose>Define the user name to obtain authorization using a password.</purpose> <usage>Set this variable to the user name of the account to authorize instead of going through the interactive user authorization process.</usage> </documentation> </variable> {/metadocument} */ var $oauth_username = ''; /* {metadocument} <variable> <name>oauth_password</name> <type>STRING</type> <value></value> <documentation> <purpose>Define the user name to obtain authorization using a password.</purpose> <usage>Set this variable to the user password of the account to authorize instead of going through the interactive user authorization process.</usage> </documentation> </variable> {/metadocument} */ var $oauth_password = ''; /* {metadocument} <variable> <name>grant_type</name> <type>STRING</type> <value>authorization_code</value> <documentation> <purpose>Define the type of grant to obtain the OAuth 2 access token.</purpose> <usage>Change this variable to <stringvalue>client_credentials</stringvalue> to obtain application only access token.<paragraphbreak /> Change this variable to <stringvalue>password</stringvalue> to obtain an access token on behalf of an user with a given username and password specified by the <variablelink>oauth_username</variablelink> and <variablelink>oauth_password</variablelink> variables respectively.<paragraphbreak />In this case the user does not need to be present, so the class will not redirect the user to the authorization dialog page.<paragraphbreak /> </usage> </documentation> </variable> {/metadocument} */ var $grant_type = "authorization_code"; var $oauth_user_agent = 'PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.125 $)'; Function SetError($error) { $this->error = $error; if($this->debug) $this->OutputDebug('Error: '.$error); return(false); } Function SetPHPError($error, &$php_error_message) { if(IsSet($php_error_message) && strlen($php_error_message)) $error.=": ".$php_error_message; return($this->SetError($error)); } Function OutputDebug($message) { if($this->debug) { $message = $this->debug_prefix.$message; $this->debug_output .= $message."\n";; error_log($message); } return(true); } Function GetRequestTokenURL(&$request_token_url) { $request_token_url = $this->request_token_url; return(true); } Function GetDialogURL(&$url, $redirect_uri = '', $state = '') { $url = (($this->offline && strlen($this->offline_dialog_url)) ? $this->offline_dialog_url : (($redirect_uri === 'oob' && strlen($this->pin_dialog_url)) ? $this->pin_dialog_url : $this->dialog_url)); if(strlen($url) === 0) return $this->SetError('the dialog URL '.($this->offline ? 'for offline access ' : '').'is not defined for this server'); $url = str_replace( '{REDIRECT_URI}', UrlEncode($redirect_uri), str_replace( '{STATE}', UrlEncode($state), str_replace( '{CLIENT_ID}', UrlEncode($this->client_id), str_replace( '{API_KEY}', UrlEncode($this->api_key), str_replace( '{SCOPE}', UrlEncode($this->scope), $url))))); return(true); } Function GetAccessTokenURL(&$access_token_url) { $access_token_url = str_replace('{API_KEY}', $this->api_key, $this->access_token_url); return(true); } Function GetStoredState(&$state) { if(!function_exists('session_start')) return $this->SetError('Session variables are not accessible in this PHP environment'); if(session_id() === '' && !session_start()) return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); if(IsSet($_SESSION['OAUTH_STATE'])) $state = $_SESSION['OAUTH_STATE']; else $state = $_SESSION['OAUTH_STATE'] = time().'-'.substr(md5(rand().time()), 0, 6); return(true); } Function GetRequestState(&$state) { $check = (strlen($this->append_state_to_redirect_uri) ? $this->append_state_to_redirect_uri : 'state'); $state = (IsSet($_GET[$check]) ? $_GET[$check] : null); return(true); } Function GetRequestCode(&$code) { $code = (IsSet($_GET['code']) ? $_GET['code'] : null); return(true); } Function GetRequestError(&$error) { $error = (IsSet($_GET['error']) ? $_GET['error'] : null); return(true); } Function GetRequestDenied(&$denied) { $denied = (IsSet($_GET['denied']) ? $_GET['denied'] : null); return(true); } Function GetRequestToken(&$token, &$verifier) { $token = (IsSet($_GET['oauth_token']) ? $_GET['oauth_token'] : null); $verifier = (IsSet($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : null); return(true); } Function GetRedirectURI(&$redirect_uri) { if(strlen($this->redirect_uri)) $redirect_uri = $this->redirect_uri; else $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; return true; } /* {metadocument} <function> <name>Redirect</name> <type>VOID</type> <documentation> <purpose>Redirect the user browser to a given page.</purpose> <usage>This function is meant to be only be called from inside the class. By default it issues HTTP 302 response status and sets the redirection location to a given URL. Sub-classes may override this function to implement a different way to redirect the user browser.</usage> </documentation> <argument> <name>url</name> <type>STRING</type> <documentation> <purpose>String with the full URL of the page to redirect.</purpose> </documentation> </argument> <do> {/metadocument} */ Function Redirect($url) { Header('HTTP/1.0 302 OAuth Redirection'); Header('Location: '.$url); } /* {metadocument} </do> </function> {/metadocument} */ /* {metadocument} <function> <name>StoreAccessToken</name> <type>BOOLEAN</type> <documentation> <purpose>Store the values of the access token when it is succefully retrieved from the OAuth server.</purpose> <usage>This function is meant to be only be called from inside the class. By default it stores access tokens in a session variable named <stringvalue>OAUTH_ACCESS_TOKEN</stringvalue>.<paragraphbreak /> Actual implementations should create a sub-class and override this function to make the access token values be stored in other types of containers, like for instance databases.</usage> <returnvalue>This function should return <booleanvalue>1</booleanvalue> if the access token was stored successfully.</returnvalue> </documentation> <argument> <name>access_token</name> <type>HASH</type> <documentation> <purpose>Associative array with properties of the access token. The array may have set the following properties:<paragraphbreak /> <stringvalue>value</stringvalue>: string value of the access token<paragraphbreak /> <stringvalue>authorized</stringvalue>: boolean value that determines if the access token was obtained successfully<paragraphbreak /> <stringvalue>expiry</stringvalue>: (optional) timestamp in ISO format relative to UTC time zone of the access token expiry time<paragraphbreak /> <stringvalue>type</stringvalue>: (optional) type of OAuth token that may determine how it should be used when sending API call requests.<paragraphbreak /> <stringvalue>refresh</stringvalue>: (optional) token that some servers may set to allowing refreshing access tokens when they expire.</purpose> </documentation> </argument> <do> {/metadocument} */ Function StoreAccessToken($access_token) { if(!function_exists('session_start')) return $this->SetError('Session variables are not accessible in this PHP environment'); if(session_id() === '' && !session_start()) return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); if(!$this->GetAccessTokenURL($access_token_url)) return false; $_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url] = $access_token; return true; } /* {metadocument} </do> </function> {/metadocument} */ /* {metadocument} <function> <name>GetAccessToken</name> <type>BOOLEAN</type> <documentation> <purpose>Retrieve the OAuth access token if it was already previously stored by the <functionlink>StoreAccessToken</functionlink> function.</purpose> <usage>This function is meant to be only be called from inside the class. By default it retrieves access tokens stored in a session variable named <stringvalue>OAUTH_ACCESS_TOKEN</stringvalue>.<paragraphbreak /> Actual implementations should create a sub-class and override this function to retrieve the access token values from other types of containers, like for instance databases.</usage> <returnvalue>This function should return <booleanvalue>1</booleanvalue> if the access token was retrieved successfully.</returnvalue> </documentation> <argument> <name>access_token</name> <type>STRING</type> <out /> <documentation> <purpose>Return the properties of the access token in an associative array. If the access token was not yet stored, it returns an empty array. Otherwise, the properties it may return are the same that may be passed to the <functionlink>StoreAccessToken</functionlink>.</purpose> </documentation> </argument> <do> {/metadocument} */ Function GetAccessToken(&$access_token) { if(!function_exists('session_start')) return $this->SetError('Session variables are not accessible in this PHP environment'); if(session_id() === '' && !session_start()) return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); if(!$this->GetAccessTokenURL($access_token_url)) return false; if(IsSet($_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url])) $access_token = $_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url]; else $access_token = array(); return true; } /* {metadocument} </do> </function> {/metadocument} */ /* {metadocument} <function> <name>ResetAccessToken</name> <type>BOOLEAN</type> <documentation> <purpose>Reset the access token to a state back when the user has not yet authorized the access to the OAuth server API.</purpose> <usage>Call this function if for some reason the token to access the API was revoked and you need to ask the user to authorize the access again.<paragraphbreak /> By default the class stores and retrieves access tokens in a session variable named <stringvalue>OAUTH_ACCESS_TOKEN</stringvalue>.<paragraphbreak /> This function must be called when the user is accessing your site pages, so it can reset the information stored in session variables that cache the state of a previously retrieved access token.<paragraphbreak /> Actual implementations should create a sub-class and override this function to reset the access token state when it is stored in other types of containers, like for instance databases.</usage> <returnvalue>This function should return <booleanvalue>1</booleanvalue> if the access token was resetted successfully.</returnvalue> </documentation> <do> {/metadocument} */ Function ResetAccessToken() { if(!$this->GetAccessTokenURL($access_token_url)) return false; if($this->debug) $this->OutputDebug('Resetting the access token status for OAuth server located at '.$access_token_url); if(!function_exists('session_start')) return $this->SetError('Session variables are not accessible in this PHP environment'); if(session_id() === '' && !session_start()) return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); if(IsSet($_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url])) Unset($_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url]); return true; } /* {metadocument} </do> </function> {/metadocument} */ Function Encode($value) { return(is_array($value) ? $this->EncodeArray($value) : str_replace('%7E', '~', str_replace('+',' ', RawURLEncode($value)))); } Function EncodeArray($array) { foreach($array as $key => $value) $array[$key] = $this->Encode($value); return $array; } Function HMAC($function, $data, $key) { switch($function) { case 'sha1': $pack = 'H40'; break; default: if($this->debug) $this->OutputDebug($function.' is not a supported an HMAC hash type'); return(''); } if(strlen($key) > 64) $key = pack($pack, $function($key)); if(strlen($key) < 64) $key = str_pad($key, 64, "\0"); return(pack($pack, $function((str_repeat("\x5c", 64) ^ $key).pack($pack, $function((str_repeat("\x36", 64) ^ $key).$data))))); } Function Sign(&$url, $method, $parameters, $oauth, $request_content_type, $has_files, $post_values_in_uri, &$authorization, &$post_values) { $values = array( 'oauth_consumer_key'=>$this->client_id, 'oauth_nonce'=>md5(uniqid(rand(), true)), 'oauth_signature_method'=>$this->signature_method, 'oauth_timestamp'=>time(), 'oauth_version'=>'1.0', ); if($has_files) $value_parameters = array(); else { if(($this->url_parameters || $method !== 'POST') && $request_content_type === 'application/x-www-form-urlencoded' && count($parameters)) { $first = (strpos($url, '?') === false); foreach($parameters as $parameter => $value) { $url .= ($first ? '?' : '&').UrlEncode($parameter).'='.UrlEncode($value); $first = false; } $parameters = array(); } $value_parameters = (($request_content_type !== 'application/x-www-form-urlencoded') ? array() : $parameters); } $header_values = ($method === 'GET' ? array_merge($values, $oauth, $value_parameters) : array_merge($values, $oauth)); $values = array_merge($values, $oauth, $value_parameters); $key = $this->Encode($this->client_secret).'&'.$this->Encode($this->access_token_secret); switch($this->signature_method) { case 'PLAINTEXT': $values['oauth_signature'] = $key; break; case 'HMAC-SHA1': $uri = strtok($url, '?'); $sign = $method.'&'.$this->Encode($uri).'&'; $first = true; $sign_values = $values; $u = parse_url($url); if(IsSet($u['query'])) { parse_str($u['query'], $q); foreach($q as $parameter => $value) $sign_values[$parameter] = $value; } KSort($sign_values); foreach($sign_values as $parameter => $value) { $sign .= $this->Encode(($first ? '' : '&').$parameter.'='.$this->Encode($value)); $first = false; } $header_values['oauth_signature'] = $values['oauth_signature'] = base64_encode($this->HMAC('sha1', $sign, $key)); break; default: return $this->SetError($this->signature_method.' signature method is not yet supported'); } if($this->authorization_header) { $authorization = 'OAuth'; $first = true; foreach($header_values as $parameter => $value) { $authorization .= ($first ? ' ' : ',').$parameter.'="'.$this->Encode($value).'"'; $first = false; } $post_values = $parameters; } else { if($method !== 'POST' || $post_values_in_uri) { $first = (strcspn($url, '?') == strlen($url)); foreach($values as $parameter => $value) { $url .= ($first ? '?' : '&').$parameter.'='.$this->Encode($value); $first = false; } $post_values = array(); } else $post_values = $values; } return true; } Function SendAPIRequest($url, $method, $parameters, $oauth, $options, &$response) { $this->response_status = 0; $http = new http_class; $http->debug = ($this->debug && $this->debug_http); $http->log_debug = true; $http->sasl_authenticate = 0; $http->user_agent = $this->oauth_user_agent; $http->redirection_limit = (IsSet($options['FollowRedirection']) ? intval($options['FollowRedirection']) : 0); $http->follow_redirect = ($http->redirection_limit != 0); if($this->debug) $this->OutputDebug('Accessing the '.$options['Resource'].' at '.$url); $post_files = array(); $method = strtoupper($method); $authorization = ''; $request_content_type = (IsSet($options['RequestContentType']) ? strtolower(trim(strtok($options['RequestContentType'], ';'))) : (($method === 'POST' || IsSet($oauth)) ? 'application/x-www-form-urlencoded' : '')); $files = (IsSet($options['Files']) ? $options['Files'] : array()); if(count($files)) { foreach($files as $name => $value) { if(!IsSet($parameters[$name])) return($this->SetError('it was specified an file parameters named '.$name)); $file = array(); switch(IsSet($value['Type']) ? $value['Type'] : 'FileName') { case 'FileName': $file['FileName'] = $parameters[$name]; break; case 'Data': $file['Data'] = $parameters[$name]; break; default: return($this->SetError($value['Type'].' is not a valid type for file '.$name)); } $file['Content-Type'] = (IsSet($value['ContentType']) ? $value['ContentType'] : 'automatic/name'); $post_files[$name] = $file; } UnSet($parameters[$name]); if($method !== 'POST') { $this->OutputDebug('For uploading files the method should be POST not '.$method); $method = 'POST'; } if($request_content_type !== 'multipart/form-data') { if(IsSet($options['RequestContentType'])) return($this->SetError('the request content type for uploading files should be multipart/form-data')); $request_content_type = 'multipart/form-data'; } } if(IsSet($oauth)) { if(!$this->Sign($url, $method, $parameters, $oauth, $request_content_type, count($files) !== 0, IsSet($options['PostValuesInURI']) && $options['PostValuesInURI'], $authorization, $post_values)) return false; } else { $post_values = $parameters; if(count($parameters)) { switch($request_content_type) { case 'application/x-www-form-urlencoded': case 'multipart/form-data': case 'application/json': break; default: $first = (strpos($url, '?') === false); foreach($parameters as $name => $value) { if(GetType($value) === 'array') { foreach($value as $index => $value) { $url .= ($first ? '?' : '&').$name.'='.UrlEncode($value); $first = false; } } else { $url .= ($first ? '?' : '&').$name.'='.UrlEncode($value); $first = false; } } } } } if(strlen($authorization) === 0 && !strcasecmp($this->access_token_type, 'Bearer')) $authorization = 'Bearer '.$this->access_token; if(strlen($error = $http->GetRequestArguments($url, $arguments))) return($this->SetError('it was not possible to open the '.$options['Resource'].' URL: '.$error)); if(strlen($error = $http->Open($arguments))) return($this->SetError('it was not possible to open the '.$options['Resource'].' URL: '.$error)); if(count($post_files)) $arguments['PostFiles'] = $post_files; $arguments['RequestMethod'] = $method; switch($request_content_type) { case 'application/x-www-form-urlencoded': case 'multipart/form-data': if(IsSet($options['RequestBody'])) return($this->SetError('the request body is defined automatically from the parameters')); $arguments['PostValues'] = $post_values; break; case 'application/json': $arguments['Headers']['Content-Type'] = $options['RequestContentType']; $arguments['Body'] = (IsSet($options['RequestBody']) ? $options['RequestBody'] : json_encode($parameters)); break; default: if(!IsSet($options['RequestBody'])) { if(IsSet($options['RequestContentType'])) return($this->SetError('it was not specified the body value of the of the API call request')); break; } $arguments['Headers']['Content-Type'] = $options['RequestContentType']; $arguments['Body'] = $options['RequestBody']; break; } $arguments['Headers']['Accept'] = (IsSet($options['Accept']) ? $options['Accept'] : '*/*'); switch($authentication = (IsSet($options['AccessTokenAuthentication']) ? strtolower($options['AccessTokenAuthentication']) : '')) { case 'basic': $arguments['Headers']['Authorization'] = 'Basic '.base64_encode($this->client_id.':'.($this->get_token_with_api_key ? $this->api_key : $this->client_secret)); break; case '': if(strlen($authorization)) $arguments['Headers']['Authorization'] = $authorization; break; default: return($this->SetError($authentication.' is not a supported authentication mechanism to retrieve an access token')); } if(IsSet($options['RequestHeaders'])) $arguments['Headers'] = array_merge($arguments['Headers'], $options['RequestHeaders']); if(strlen($error = $http->SendRequest($arguments)) || strlen($error = $http->ReadReplyHeaders($headers))) { $http->Close(); return($this->SetError('it was not possible to retrieve the '.$options['Resource'].': '.$error)); } $error = $http->ReadWholeReplyBody($data); $http->Close(); if(strlen($error)) { return($this->SetError('it was not possible to access the '.$options['Resource'].': '.$error)); } $this->response_status = intval($http->response_status); $content_type = (IsSet($options['ResponseContentType']) ? $options['ResponseContentType'] : (IsSet($headers['content-type']) ? strtolower(trim(strtok($headers['content-type'], ';'))) : 'unspecified')); $content_type = preg_replace('/^(.+\\/).+\\+(.+)$/', '\\1\\2', $content_type); switch($content_type) { case 'text/javascript': case 'application/json': if(!function_exists('json_decode')) return($this->SetError('the JSON extension is not available in this PHP setup')); $object = json_decode($data); switch(GetType($object)) { case 'object': if(!IsSet($options['ConvertObjects']) || !$options['ConvertObjects']) $response = $object; else { $response = array(); foreach($object as $property => $value) $response[$property] = $value; } break; case 'array': $response = $object; break; default: if(!IsSet($object)) return($this->SetError('it was not returned a valid JSON definition of the '.$options['Resource'].' values')); $response = $object; break; } break; case 'application/x-www-form-urlencoded': case 'text/plain': case 'text/html': parse_str($data, $response); break; case 'text/xml': if(IsSet($options['DecodeXMLResponse'])) { switch(strtolower($options['DecodeXMLResponse'])) { case 'simplexml': if($this->debug) $this->OutputDebug('Decoding XML response with simplexml'); try { $response = @new SimpleXMLElement($data); } catch(Exception $exception) { return $this->SetError('Could not parse XML response: '.$exception->getMessage()); } break 2; default: return $this->SetError($options['DecodeXML'].' is not a supported method to decode XML responses'); } } default: $response = $data; break; } if($this->response_status >= 200 && $this->response_status < 300) $this->access_token_error = ''; else { $this->access_token_error = 'it was not possible to access the '.$options['Resource'].': it was returned an unexpected response status '.$http->response_status.' Response: '.$data; if($this->debug) $this->OutputDebug('Could not retrieve the OAuth access token. Error: '.$this->access_token_error); if(IsSet($options['FailOnAccessError']) && $options['FailOnAccessError']) { $this->error = $this->access_token_error; return false; } } return true; } Function ProcessToken1($oauth, &$access_token) { if(!$this->GetAccessTokenURL($url)) return false; $options = array('Resource'=>'OAuth access token'); $method = strtoupper($this->token_request_method); switch($method) { case 'GET': break; case 'POST': $options['PostValuesInURI'] = true; break; default: $this->error = $method.' is not a supported method to request tokens'; return false; } if(!$this->SendAPIRequest($url, $method, array(), $oauth, $options, $response)) return false; if(strlen($this->access_token_error)) { $this->authorization_error = $this->access_token_error; return true; } if(!IsSet($response['oauth_token']) || !IsSet($response['oauth_token_secret'])) { $this->authorization_error= 'it was not returned the access token and secret'; return true; } $access_token = array( 'value'=>$response['oauth_token'], 'secret'=>$response['oauth_token_secret'], 'authorized'=>true ); if(IsSet($response['oauth_expires_in']) && $response['oauth_expires_in'] == 0) { if($this->debug) $this->OutputDebug('Ignoring access token expiry set to 0'); $this->access_token_expiry = ''; } elseif(IsSet($response['oauth_expires_in'])) { $expires = $response['oauth_expires_in']; if(strval($expires) !== strval(intval($expires)) || $expires <= 0) return($this->SetError('OAuth server did not return a supported type of access token expiry time')); $this->access_token_expiry = gmstrftime('%Y-%m-%d %H:%M:%S', time() + $expires); if($this->debug) $this->OutputDebug('Access token expiry: '.$this->access_token_expiry.' UTC'); $access_token['expiry'] = $this->access_token_expiry; } else $this->access_token_expiry = ''; if(IsSet($response['oauth_session_handle'])) { $access_token['refresh'] = $response['oauth_session_handle']; if($this->debug) $this->OutputDebug('Refresh token: '.$access_token['refresh']); } return $this->StoreAccessToken($access_token); } Function ProcessToken2($code, $refresh) { if(!$this->GetRedirectURI($redirect_uri)) return false; $authentication = $this->access_token_authentication; if(strlen($this->oauth_username)) { $values = array( 'grant_type'=>'password', 'username'=>$this->oauth_username, 'password'=>$this->oauth_password, 'redirect_uri' => $redirect_uri ); $authentication = 'Basic'; } elseif($this->redirect_uri === 'oob' && strlen($this->pin)) { $values = array( 'grant_type'=>'pin', 'pin'=>$this->pin, 'scope'=>$this->scope, ); } elseif($refresh) { $values = array( 'refresh_token'=>$this->refresh_token, 'grant_type'=>'refresh_token', 'scope'=>$this->scope, ); } else { switch($this->grant_type) { case 'password': return $this->SetError('it was not specified the username for obtaining a password based OAuth 2 authorization'); case 'authorization_code': $values = array( 'code'=>$code, 'redirect_uri'=>$redirect_uri, 'grant_type'=>'authorization_code' ); break; case 'client_credentials': $values = array( 'grant_type'=>'client_credentials' ); $authentication = 'Basic'; break; default: return $this->SetError($this->grant_type.' is not yet a supported OAuth 2 grant type'); } } $options = array( 'Resource'=>'OAuth '.($refresh ? 'refresh' : 'access').' token', 'ConvertObjects'=>true ); switch(strtolower($authentication)) { case 'basic': $options['AccessTokenAuthentication'] = $authentication; break; case '': $values['client_id'] = $this->client_id; $values['client_secret'] = ($this->get_token_with_api_key ? $this->api_key : $this->client_secret); break; default: return($this->SetError($authentication.' is not a supported authentication mechanism to retrieve an access token')); } if(!$this->GetAccessTokenURL($access_token_url)) return false; if(!$this->SendAPIRequest($access_token_url, 'POST', $values, null, $options, $response)) return false; if(strlen($this->access_token_error)) { $this->authorization_error = $this->access_token_error; return true; } if(!IsSet($response['access_token'])) { if(IsSet($response['error'])) { $this->authorization_error = 'it was not possible to retrieve the access token: it was returned the error: '.$response['error']; return true; } return($this->SetError('OAuth server did not return the access token')); } $access_token = array( 'value'=>($this->access_token = $response['access_token']), 'authorized'=>true, ); if($this->store_access_token_response) $access_token['response'] = $this->access_token_response = $response; if($this->debug) $this->OutputDebug('Access token: '.$this->access_token); if(IsSet($response['expires_in']) && $response['expires_in'] == 0) { if($this->debug) $this->OutputDebug('Ignoring access token expiry set to 0'); $this->access_token_expiry = ''; } elseif(IsSet($response['expires']) || IsSet($response['expires_in'])) { $expires = (IsSet($response['expires']) ? $response['expires'] : $response['expires_in']); if(strval($expires) !== strval(intval($expires)) || $expires <= 0) return($this->SetError('OAuth server did not return a supported type of access token expiry time')); $this->access_token_expiry = gmstrftime('%Y-%m-%d %H:%M:%S', time() + $expires); if($this->debug) $this->OutputDebug('Access token expiry: '.$this->access_token_expiry.' UTC'); $access_token['expiry'] = $this->access_token_expiry; } else $this->access_token_expiry = ''; if(IsSet($response['token_type'])) { $this->access_token_type = $response['token_type']; if(strlen($this->access_token_type) && $this->debug) $this->OutputDebug('Access token type: '.$this->access_token_type); $access_token['type'] = $this->access_token_type; } else { $this->access_token_type = $this->default_access_token_type; if(strlen($this->access_token_type) && $this->debug) $this->OutputDebug('Assumed the default for OAuth access token type which is '.$this->access_token_type); } if(IsSet($response['refresh_token'])) { $this->refresh_token = $response['refresh_token']; if($this->debug) $this->OutputDebug('Refresh token: '.$this->refresh_token); $access_token['refresh'] = $this->refresh_token; } elseif(strlen($this->refresh_token)) { if($this->debug) $this->OutputDebug('Reusing previous refresh token: '.$this->refresh_token); $access_token['refresh'] = $this->refresh_token; } return $this->StoreAccessToken($access_token); } Function RetrieveToken(&$valid) { $valid = false; if(!$this->GetAccessToken($access_token)) return false; if(IsSet($access_token['value'])) { $this->access_token_expiry = ''; $expired = (IsSet($access_token['expiry']) && strcmp($this->access_token_expiry = $access_token['expiry'], gmstrftime('%Y-%m-%d %H:%M:%S')) < 0); if($expired) { if($this->debug) $this->OutputDebug('The OAuth access token expired on '.$this->access_token_expiry.' UTC'); } $this->access_token = $access_token['value']; if(!$expired && $this->debug) $this->OutputDebug('The OAuth access token '.$this->access_token.' is valid'); if(IsSet($access_token['type'])) { $this->access_token_type = $access_token['type']; if(strlen($this->access_token_type) && !$expired && $this->debug) $this->OutputDebug('The OAuth access token is of type '.$this->access_token_type); } else { $this->access_token_type = $this->default_access_token_type; if(strlen($this->access_token_type) && !$expired && $this->debug) $this->OutputDebug('Assumed the default for OAuth access token type which is '.$this->access_token_type); } if(IsSet($access_token['secret'])) { $this->access_token_secret = $access_token['secret']; if($this->debug && !$expired && strlen($this->access_token_secret)) $this->OutputDebug('The OAuth access token secret is '.$this->access_token_secret); } if(IsSet($access_token['refresh'])) $this->refresh_token = $access_token['refresh']; else $this->refresh_token = ''; $this->access_token_response = (($this->store_access_token_response && IsSet($access_token['response'])) ? $access_token['response'] : null); $val
![]() Are you still experiencing this problem?
![]() yes.some times i got error.after clear my browser history after try to run the code.code is working good..
![]() Are you making frequent calls to the Meetup API?
I have uploaded a new version of the class that computes the expiry time more accurately. Still if your machine must have the time set accurately, or else it may happen that the token expired but your machine time is not correct to the point of assuming the token did not expire yet. Just let me know if you still experience the problem with the class latest version. |
info at phpclasses dot org
.