Overview

Classes

  • Dropbox\AccessToken
  • Dropbox\AccessType
  • Dropbox\AppInfo
  • Dropbox\AuthInfo
  • Dropbox\Client
  • Dropbox\Config
  • Dropbox\Path
  • Dropbox\RequestToken
  • Dropbox\Token
  • Dropbox\WebAuth
  • Dropbox\WriteMode

Exceptions

  • Dropbox\AppInfoLoadException
  • Dropbox\AuthInfoLoadException
  • Dropbox\DeserializeException
  • Dropbox\Exception
  • Dropbox\Exception_BadRequest
  • Dropbox\Exception_BadResponse
  • Dropbox\Exception_BadResponseCode
  • Dropbox\Exception_InvalidAccessToken
  • Dropbox\Exception_NetworkIO
  • Dropbox\Exception_ProtocolError
  • Dropbox\Exception_RetryLater
  • Dropbox\Exception_ServerError
  • Overview
  • Class
  • Tree
 1: <?php
 2: namespace Dropbox;
 3: 
 4: /**
 5:  * This class contains methods to load an AppInfo and AccessToken from a JSON file.
 6:  * This can help simplify simple scripts (such as the example programs that come with the
 7:  * SDK) but is probably not useful in typical Dropbox API apps.
 8:  *
 9:  */
10: final class AuthInfo
11: {
12:     /**
13:      * Loads a JSON file containing authorization information for your app. 'php authorize.php'
14:      * in the examples directory for details about what this file should look like.
15:      *
16:      * @param string $path
17:      *    Path to a JSON file
18:      * @return array
19:      *    A pair of (AppInfo $appInfo, AccessToken $accessToken).
20:      *
21:      * @throws AuthInfoLoadException
22:      */
23:     static function loadFromJsonFile($path)
24:     {
25:         if (!file_exists($path)) {
26:             throw new AuthInfoLoadException("File doesn't exist: \"$path\"");
27:         }
28: 
29:         $str = file_get_contents($path);
30:         $jsonArr = json_decode($str, TRUE);
31: 
32:         if (is_null($jsonArr)) {
33:             throw new AuthInfoLoadException("JSON parse error: \"$path\"");
34:         }
35: 
36:         return self::loadFromJson($jsonArr);
37:     }
38: 
39:     /**
40:      * Parses a JSON object to build an AuthInfo object.  If you would like to load this from a file,
41:      * please use the @see loadFromJsonFile method.
42:      *
43:      * @param array $jsonArr
44:      *    A parsed JSON object, typcally the result of json_decode(..., TRUE).
45:      * @return array
46:      *    A list(AppInfo $appInfo, AccessToken $accessToken).
47:      *
48:      * @throws AuthInfoLoadException
49:      */
50:     private static function loadFromJson($jsonArr)
51:     {
52:         if (!is_array($jsonArr)) {
53:             throw new AuthInfoLoadException("Expecting JSON object, found something else");
54:         }
55: 
56:         if (!isset($jsonArr['app'])) {
57:             throw new AuthInfoLoadException("Missing field \"app\"");
58:         }
59: 
60:         // Extract app info
61:         $appJson = $jsonArr['app'];
62: 
63:         try {
64:             $appInfo = AppInfo::loadFromJson($appJson);
65:         }
66:         catch (AppInfoLoadException $e) {
67:             throw new AuthInfoLoadException("Bad \"app\" field: ".$e->getMessage());
68:         }
69: 
70:         // Extract access token
71:         if (!isset($jsonArr['access_token'])) {
72:             throw new AuthInfoLoadException("Missing field \"access_token\"");
73:         }
74: 
75:         $accessTokenString = $jsonArr['access_token'];
76:         if (!is_string($accessTokenString)) {
77:             throw new AuthInfoLoadException("Expecting field \"access_token\" to be a string");
78:         }
79: 
80:         $accessToken = AccessToken::deserialize($accessTokenString);
81: 
82:         return array($appInfo, $accessToken);
83:     }
84: }
85: 
Dropbox SDK for PHP API documentation generated by ApiGen 2.8.0