Skip to content
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

FEATURE: Have ability to run pcloud on headless server/terminal #48

Closed
ikwyl6 opened this issue Sep 14, 2021 · 6 comments
Closed

FEATURE: Have ability to run pcloud on headless server/terminal #48

ikwyl6 opened this issue Sep 14, 2021 · 6 comments

Comments

@ikwyl6
Copy link

ikwyl6 commented Sep 14, 2021

Tried pcloud using oauth, which works fine, but then the API is looking for a firefox browser or similar on a desktop/GUI. The script should be able to run within console/terminal (headless server). See output below:

$ python          
Python 3.9.6 (default, Jul  6 2021, 19:38:26)                                                     
[GCC 10.2.0] on linux                           
Type "help", "copyright", "credits" or "license" for more information.
>>> from pcloud import PyCloud
>>> pc = api.PyCloud.oauth2_authorize(client_id="MY_CID", client_secret="MY_CS")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'api' is not defined
>>> from pcloud import api                                                                         
>>> pc = api.PyCloud.oauth2_authorize(client_id="MY_CID", client_secret="MY_CS")
/usr/bin/xdg-open: line 881: x-www-browser: command not found
/usr/bin/xdg-open: line 881: firefox: command not found
/usr/bin/xdg-open: line 881: iceweasel: command not found
/usr/bin/xdg-open: line 881: seamonkey: command not found
/usr/bin/xdg-open: line 881: mozilla: command not found
/usr/bin/xdg-open: line 881: epiphany: command not found
/usr/bin/xdg-open: line 881: konqueror: command not found
/usr/bin/xdg-open: line 881: chromium: command not found
/usr/bin/xdg-open: line 881: chromium-browser: command not found
/usr/bin/xdg-open: line 881: google-chrome: command not found
/usr/bin/xdg-open: line 881: www-browser: command not found
/usr/bin/xdg-open: line 881: links2: command not found
/usr/bin/xdg-open: line 881: elinks: command not found
/usr/bin/xdg-open: line 881: links: command not found
/usr/bin/xdg-open: line 881: lynx: command not found
/usr/bin/xdg-open: line 881: w3m: command not found
xdg-open: no method available for opening 'https://my.pcloud.com/oauth2/authorize?response_type=code&redirect_uri=http://localhost:65432/&client_id=MY_CID'
@tomgross
Copy link
Owner

You can use username/password authentication (see https://github.com/tomgross/pycloud#usage-of-api) instead of OAuth 2.0 authentication.
OAuth 2.0 is designed to include a web page in its flow (see https://docs.pcloud.com/methods/oauth_2.0/authorize.html).

What you can try is to generate an access token somehow and pass it to PyCloud directly. As documented, the access token currently will not expire (see https://docs.pcloud.com/methods/oauth_2.0/)

>>> access_token = "..."
>>> pc = PyCloud("", access_token, oauth2=True)

@ikwyl6
Copy link
Author

ikwyl6 commented May 7, 2022

Is there a way to get the browser redirect address and then the user can plug that into a webbrowser? Then take the token code and paste it back into the script on the command line and then it gets the access_token that way? That is how some of the other cloud-type scripts work to gain access via command line.

@ikwyl6
Copy link
Author

ikwyl6 commented Jun 25, 2022

I've been looking at your code and would you entertain a PR that would give the user the ability to run this on a headless server, the script outputs the link to input into a browser (laptop) and then the access token that is returned from the request_uri can be pasted back into a users script of pycloud that authenicates them...? This is done this way in a few different scripts I've seen that interacts with box.com, dropbox, etc, but on a headless server and you do any webbrowsing interaction on another computer (laptop/desktop)...

@ikwyl6
Copy link
Author

ikwyl6 commented Jul 17, 2022

Hi to use a headless-type server..I've created a script that includes a TokenHandler class in the same file as my script but when I pass my TokenHandler into PyCloud.oauth2_authorize like so:

class TokenHandler:
    redirect_url = "localhost"

    def __init__(self, client_id):
        self._id = client_id
        self.auth_url = f"https://my.pcloud.com/oauth2/authorize?response_type=code&redirect_uri={self.redirect_url}&client_id={self._id}"
        #print(self.auth_url)

    def get_access_token(self):
        print("Copy url into browser: {0}".format(self.auth_url))
        access_token = input("\nCopy/paste the 'code' portion of the link in address bar here: ")
        return access_token, "localhost"

th = TokenHandler(client_id)

pc = PyCloud.oauth2_authorize(client_id=client_id,
        client_secret=client_secret, tokenhandler=th)

I get a

File "/home/alarm/.local/lib/python3.10/site-packages/pcloud/api.py", line 114, in oauth2_authorize
code, hostname = tokenhandler(client_id).get_access_token()
TypeError: 'TokenHandler' object is not callable

I know this might be more of a python-type question, but I thought you may be able to help me out faster as I don't have the most experience with python and classes, etc.

@ikwyl6
Copy link
Author

ikwyl6 commented Jul 17, 2022

In order for the user to create their own TokenHandler class, I think you need to make the endpoint var available to pass into oauth2_authorize as a variable so the headless server can set endpoint.

@ikwyl6
Copy link
Author

ikwyl6 commented Jul 18, 2022

you can disregard the above two comments. I figured out after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants