rails-pinterest 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -2
- data/Gemfile.lock +1 -1
- data/README.md +63 -0
- data/lib/pinterest/client.rb +7 -0
- data/lib/pinterest/http.rb +25 -0
- data/lib/pinterest/oauth.rb +11 -0
- data/lib/pinterest/version.rb +1 -1
- data/lib/pinterest.rb +6 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1fc96a443db45b6ac15f51c8152b6e87145b1c6dacb58d5b465b2d8a69f8b82
|
4
|
+
data.tar.gz: 6ea256a6db07492035e6e622f4cb7e5617792ef3963ec7a1e452a9406a7279ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12bad857ab4b1aec512620322dc720c0e04243537279cad50c5be6242208c2aa3a264ef0ac82cbd87892da2f5276a7d7993f501d0fdac397718e0a0a580653a8
|
7
|
+
data.tar.gz: 8c3a3ac50db6366fa37df5d5aacff6d8e556aa6545bbb7350c650ceb7096efa8999869427c7798773b7022cb559183f230167490330d4c6422d473ea2f355db7
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
## [0.1.0] - 2023-
|
3
|
+
## [0.1.0] - 2023-11-30
|
4
4
|
|
5
5
|
- Initial release
|
6
|
-
- Endpoints for Pins and Boards created
|
6
|
+
- Endpoints for Pins and Boards created
|
7
|
+
|
8
|
+
## [0.1.1] - 2023-11-30
|
9
|
+
- Update the gemfile.lock
|
10
|
+
- Push the tagged version to github
|
11
|
+
- Re-release to rubygems
|
12
|
+
|
13
|
+
## [0.1.2] - 2023-12-2
|
14
|
+
- Added oauth refresh token endpoint.
|
15
|
+
|
16
|
+
## [0.1.3] - 2023-12-2
|
17
|
+
- Add to ReadMe how to get access token from a authorization token
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -174,6 +174,69 @@ response = client.pins.create_pin(parameters: parameters)
|
|
174
174
|
|
175
175
|
```
|
176
176
|
|
177
|
+
### Oauth
|
178
|
+
|
179
|
+
How to get exchange your authorization code for an access token [(step 3)](https://developers.pinterest.com/docs/getting-started/authentication/)
|
180
|
+
```ruby
|
181
|
+
# https://developers.pinterest.com/docs/getting-started/authentication/
|
182
|
+
|
183
|
+
# POST /oauth/tokens to exchange authorization code for access token
|
184
|
+
|
185
|
+
# 1. Build the client
|
186
|
+
client = Pinterest::Client.new(access_token: "fake-token-because-you-dont-have-this-yet", client_id: "client-id" , secret_key: "secret-key")
|
187
|
+
|
188
|
+
# 2. Build parameters
|
189
|
+
parameters = {
|
190
|
+
'grant_type' => 'authorization_code', # Required from Pinterest
|
191
|
+
'code' => params[:code].to_s, # Replace with the actual authorization code
|
192
|
+
'redirect_uri' => "your-redirect-uri"
|
193
|
+
}
|
194
|
+
|
195
|
+
# 3. Get the access token
|
196
|
+
response = client.oauth.retrieve_oauth_token(parameters: parameters)
|
197
|
+
puts response
|
198
|
+
# { "access_token": "{an access token string prefixed with 'pina'}", "refresh_token": "{a refresh token string prefixed with 'pinr'}", "response_type": "authorization_code", ..
|
199
|
+
|
200
|
+
```
|
201
|
+
|
202
|
+
How to refresh your access tokens
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
# https://developers.pinterest.com/docs/api/v5/#tag/oauth
|
206
|
+
|
207
|
+
# POST /oauth/tokens to refresh tokens
|
208
|
+
parameters = {
|
209
|
+
refresh_token: "your-refresh-token-goes-here",
|
210
|
+
grant_type: 'refresh_token' # Keep it as 'refresh_token' as per Pinterest API requirements
|
211
|
+
scope: 'whatever-new-scopes-desired' #optional
|
212
|
+
}
|
213
|
+
response = client.oauth.retrieve_oauth_token(parameters: parameters)
|
214
|
+
puts response
|
215
|
+
# { "response_type": "refresh_token", "access_token": "string", "token_type": "bearer", "expires_in": 0, "scope": "string" }
|
216
|
+
|
217
|
+
```
|
218
|
+
|
219
|
+
Running the Oauth Refresh Token in Console
|
220
|
+
```ruby
|
221
|
+
# https://developers.pinterest.com/docs/api/v5/#tag/oauth
|
222
|
+
|
223
|
+
# 1. Create the client
|
224
|
+
client = Pinterest::Client.new(refresh_token: "your-refresh-token", access_token: "can-be-any-fake-or-real-token", client_id: 'client-id-or-app-id' , secret_key: "secret-key")
|
225
|
+
|
226
|
+
# 2. Set up the params
|
227
|
+
parameters = {
|
228
|
+
refresh_token: "your-refresh-token-goes-here",
|
229
|
+
grant_type: 'refresh_token' # Keep it as 'refresh_token' as per Pinterest API requirements
|
230
|
+
scope: 'whatever-new-scopes-desired' #optional
|
231
|
+
}
|
232
|
+
|
233
|
+
# 3. Retrieve refreshed access_token
|
234
|
+
response = client.oauth.retrieve_oauth_token(parameters: parameters)
|
235
|
+
puts response
|
236
|
+
# { "response_type": "refresh_token", "access_token": "string", "token_type": "bearer", "expires_in": 0, "scope": "string" }
|
237
|
+
|
238
|
+
```
|
239
|
+
**NOTE:** This is likely how you'll be doing it if you manage multiple Pinterest Apps for users in your web/mobile app. Because you'll have multiple access_tokens and refresh_tokens, you can't have this in an .env configuration. You'll likely create new clients every time you refresh with your users' or customers' saved access/refresh_tokens.
|
177
240
|
## Development
|
178
241
|
|
179
242
|
After checking out the repo, run `bin/setup` to install dependencies. You can run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/pinterest/client.rb
CHANGED
@@ -6,6 +6,9 @@ module Pinterest
|
|
6
6
|
api_type
|
7
7
|
api_version
|
8
8
|
access_token
|
9
|
+
refresh_token
|
10
|
+
client_id
|
11
|
+
secret_key
|
9
12
|
uri_base
|
10
13
|
request_timeout
|
11
14
|
extra_headers
|
@@ -27,5 +30,9 @@ module Pinterest
|
|
27
30
|
def pins
|
28
31
|
@pins ||= Pinterest::Pins.new(client: self)
|
29
32
|
end
|
33
|
+
|
34
|
+
def oauth
|
35
|
+
@oauth ||= Pinterest::Oauth.new(client: self)
|
36
|
+
end
|
30
37
|
end
|
31
38
|
end
|
data/lib/pinterest/http.rb
CHANGED
@@ -34,6 +34,14 @@ module Pinterest
|
|
34
34
|
end&.body)
|
35
35
|
end
|
36
36
|
|
37
|
+
def oauth_post(path:, parameters:)
|
38
|
+
response = oauth_conn.post(uri(path: path)) do |req|
|
39
|
+
req.headers = oauth_headers
|
40
|
+
req.body = parameters
|
41
|
+
end
|
42
|
+
response
|
43
|
+
end
|
44
|
+
|
37
45
|
private
|
38
46
|
|
39
47
|
def to_json(string)
|
@@ -52,6 +60,14 @@ module Pinterest
|
|
52
60
|
end
|
53
61
|
end
|
54
62
|
|
63
|
+
def oauth_conn(multipart: false)
|
64
|
+
Faraday.new do |f|
|
65
|
+
f.options[:timeout] = @request_timeout
|
66
|
+
f.request :url_encoded
|
67
|
+
f.adapter Faraday.default_adapter
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
55
71
|
def uri(path:)
|
56
72
|
File.join(@uri_base, @api_version, path)
|
57
73
|
end
|
@@ -67,6 +83,15 @@ module Pinterest
|
|
67
83
|
}
|
68
84
|
end
|
69
85
|
|
86
|
+
def oauth_headers
|
87
|
+
token = Base64.encode64("#{@client_id}:#{@secret_key}").gsub("\n", '')
|
88
|
+
puts "Token: #{token}"
|
89
|
+
{
|
90
|
+
"Authorization" => "Basic #{token}",
|
91
|
+
"Content-Type" => "application/x-www-form-urlencoded",
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
70
95
|
def multipart_parameters(parameters)
|
71
96
|
parameters&.transform_values do |value|
|
72
97
|
next value unless value.respond_to?(:close) # File or IO object.
|
data/lib/pinterest/version.rb
CHANGED
data/lib/pinterest.rb
CHANGED
@@ -3,6 +3,7 @@ require "faraday/multipart"
|
|
3
3
|
require_relative "pinterest/http"
|
4
4
|
require_relative "pinterest/client"
|
5
5
|
require_relative "pinterest/boards"
|
6
|
+
require_relative "pinterest/oauth"
|
6
7
|
require_relative "pinterest/pins"
|
7
8
|
require_relative "pinterest/version"
|
8
9
|
|
@@ -13,7 +14,7 @@ module Pinterest
|
|
13
14
|
class Configuration
|
14
15
|
attr_writer :access_token
|
15
16
|
attr_accessor :api_type, :api_version, :uri_base, :request_timeout,
|
16
|
-
:extra_headers
|
17
|
+
:extra_headers, :refresh_token, :client_id, :secret_key
|
17
18
|
|
18
19
|
DEFAULT_API_VERSION = "v5".freeze
|
19
20
|
DEFAULT_URI_BASE = "https://api.pinterest.com/".freeze
|
@@ -22,6 +23,9 @@ module Pinterest
|
|
22
23
|
def initialize
|
23
24
|
@access_token = nil
|
24
25
|
@api_type = nil
|
26
|
+
@refresh_token = nil
|
27
|
+
@client_id = nil
|
28
|
+
@secret_key = nil
|
25
29
|
@api_version = DEFAULT_API_VERSION
|
26
30
|
@uri_base = DEFAULT_URI_BASE
|
27
31
|
@request_timeout = DEFAULT_REQUEST_TIMEOUT
|
@@ -34,7 +38,7 @@ module Pinterest
|
|
34
38
|
def access_token
|
35
39
|
return @access_token if @access_token
|
36
40
|
|
37
|
-
error_text = "Pinterest access token missing!
|
41
|
+
error_text = "Pinterest access token missing!"
|
38
42
|
raise ConfigurationError, error_text
|
39
43
|
end
|
40
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-pinterest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Donald Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- lib/pinterest/client.rb
|
61
61
|
- lib/pinterest/compatibility.rb
|
62
62
|
- lib/pinterest/http.rb
|
63
|
+
- lib/pinterest/oauth.rb
|
63
64
|
- lib/pinterest/pins.rb
|
64
65
|
- lib/pinterest/version.rb
|
65
66
|
- lib/ruby/pinterest.rb
|