Skip to content

Move interactive prompt to generate access token into client #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ Or install it yourself as:

### Generating an access token

To use the `pcloud_api` client, you will need to first generate an access token. You may do this by cloning and running the script in [`./bin/generate_access_token`](https://github.com/jhunschejones/pcloud_api/blob/master/bin/generate_access_token). It will take you through the process of setting up a pCloud app and completing the OAuth2 flow in the browser.
To use the `pcloud_api` client, you will need to first generate an access token. You may do this by opening a Rails console or IRB session and entering the following code:

```ruby
irb(main):001:0> require "pcloud_api"
=> true
irb(main):002:0> Pcloud::Client.generate_access_token
=== Follow these steps to generate a pCloud app and access token ===
...
```

You will be presented with an interactive prompt which will take you through the process of setting up a pCloud app and completing the OAuth2 flow in the browser. At the end of the prompt, your new access token will be displayed. You should save this value in a secure location as it can be used to access your pCloud account data.

### Configuration

Expand Down
46 changes: 0 additions & 46 deletions bin/generate_access_token

This file was deleted.

35 changes: 35 additions & 0 deletions lib/pcloud/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ def execute(method, query: {}, body: {})
json_response
end

def generate_access_token
puts "=== Follow these steps to generate a pCloud app and access token ==="
puts "1. Register an app at `https://docs.pcloud.com/my_apps/`"

puts "2. Enter the client id and secret for the app:"
print "Client ID > "
client_id = $stdin.gets.chomp

print "Client Secret > "
client_secret = $stdin.gets.chomp

puts "3. Enter the data region of your pCloud account [EU/US]:"
print "> "
region_specific_api_base = $stdin.gets.chomp == "EU" ? "eapi.pcloud.com" : "api.pcloud.com"

puts "4. Navigate to this URL to start the access code flow:"
puts "`https://my.pcloud.com/oauth2/authorize?client_id=#{client_id}&response_type=code`"
puts "5. After logging in, enter the access code provided below:"
print "> "
access_code = $stdin.gets.chomp

puts "6. Requesting access token from pCloud..."
query = { client_id: client_id, client_secret: client_secret, code: access_code }
response = HTTParty.post(
"https://#{region_specific_api_base}/oauth2_token?#{URI.encode_www_form(query)}",
headers: { "Accept" => "application/json" }
)

json_response = JSON.parse(response.body)
raise json_response["error"] if json_response["error"]
puts "Done! Your access token is: \n#{json_response["access_token"]}"
puts "\nStore this value somewhere secure as it can be used to access your"
puts "pCloud account data and it will not be shown again."
end

private

def data_region_from_config_or_env
Expand Down
2 changes: 2 additions & 0 deletions lib/pcloud_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "tzinfo"
require "json"
require "time"
require "json"
require "uri"

require "pcloud/version"
require "pcloud/time_helper"
Expand Down
34 changes: 34 additions & 0 deletions spec/pcloud/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,40 @@
end
end

describe ".generate_access_token" do
let(:httparty_response) { double(HTTParty::Response) }

before do
# silence console output from the interactive CLI
allow(Pcloud::Client).to receive(:puts)
allow(Pcloud::Client).to receive(:print)

allow(httparty_response).to receive(:body).and_return({ access_token: "Here's your access token!" }.to_json)
allow(HTTParty).to receive(:post).and_return(httparty_response)
end

it "makes the expected web request to get an access token" do
client_id = "my_client_id"
client_secret = "my_client_secret"
access_code = "pcloud_access_code"

allow($stdin).to receive(:gets).and_return(
client_id,
client_secret,
"EU", # user specified data region
access_code, # access code provided by pCloud
)
expect(HTTParty)
.to receive(:post)
.with(
"https://eapi.pcloud.com/oauth2_token?client_id=#{client_id}&client_secret=#{client_secret}&code=#{access_code}",
{ headers: { "Accept" => "application/json" } }
).and_return(httparty_response)

Pcloud::Client.generate_access_token
end
end

describe ".data_region_from_config_or_env" do
it "reads from module configuration" do
Pcloud::Client.configure(access_token: "test-token", data_region: "EU")
Expand Down