· 6 years ago · Nov 30, 2018, 01:20 PM
1<?php
2// include the Tumblr PHP file. The directory is up to your choosing.
3include ("lib/tumblrPHP.php");
4
5// Enter your Consumer / Secret Key:
6$consumer = "CONSUMER_KEY";
7$secret = "SECRET_KEY";
8
9// Start the Session
10session_start();
11
12
13// If we already have the oauth tokens set by the Session - let's use them to make a request
14if ((isset($_SESSION['oauth_token']) && isset($_SESSION['oauth_token_secret'])))
15{
16 // set the Content Type to JSON (so we can use things like JSON Viewer in Firefox to view the response nicely)
17 header("Content-type: application/json");
18
19 // Create an instance of the Tumblr Class passing the consumer, secret, the access token for the user
20 $tumblr = new Tumblr($consumer, $secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
21
22 // Grab the followers by using the oauth_get method.
23 $followers = $tumblr->oauth_get("/blog/blog.untappd.com/followers");
24
25 // Print the results to the screen
26 echo json_encode($followers);
27}
28// If we detect the oauth_token is from the query string (this is after the user authorizes the account)
29else if (isset($_GET['oauth_token'])) {
30
31 // set the Content Type to JSON (so we can use things like JSON Viewer in Firefox to view the response nicely)
32 header("Content-type: application/json");
33
34 // Set up the instance of the class using the session REQUEST tokens set at line 51 and 52
35 $tumblr = new Tumblr($consumer, $secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
36
37 // The oauth_verfier is set back from Tumblr and is needed to obtain access tokens
38 $oauth_verifier = $_GET['oauth_verifier'];
39
40 // User the getAcessToken method and pass through the oauth_verifier to get tokens;
41 $token = $tumblr->getAcessToken($oauth_verifier);
42
43 // Set the session for the new access tokens, replacing the request tokens
44 $_SESSION['oauth_token'] = $token['oauth_token'];
45 $_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
46
47 // Create a new instance of the Tumblr Class with the Request Tokens that we just set at line 40 and 41
48 $tumblr = new Tumblr($consumer, $secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
49
50 // Grab the likes of the authenticated user.
51 $likes = $tumblr->oauth_get("/users/likes");
52
53 // Print the results to the screen
54 echo json_encode($likes);
55}
56// If this is the first time we launch this page - it will need to auto redirect to the Tumblr site for account access approval
57else
58{
59 // Create a new instance of the Tumblr Class with your Conumser and Secret when you create your app.
60 $tumblr = new Tumblr($consumer, $secret);
61
62 // Get the request tokens based on your consumer and secret and store them in $token
63 $token = $tumblr->getRequestToken();
64
65 // Set session of those request tokens so we can use them after the application passes back to your callback URL
66 $_SESSION['oauth_token'] = $token['oauth_token'];
67 $_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
68
69 // Grab the Authorize URL and pass through the variable of the oauth_token
70 $data = $tumblr->getAuthorizeURL($token['oauth_token']);
71
72 // The user will be directed to the "Allow Access" screen on Tumblr
73 header("Location: " . $data);
74}
75
76
77?>