fniephaus / alfred-gmail

Manage your Gmail inbox with Alfred
MIT License
76 stars 7 forks source link

Detect and use system proxy #18

Open sepehr opened 7 years ago

sepehr commented 7 years ago

Currently, the workflow's python script does not respect the globally set proxy:

20:25:11 workflow.py:2133 DEBUG    Last run version : 1.2.0
20:25:11 workflow.py:2181 DEBUG    update_data : None
20:25:11 workflow.py:2350 DEBUG    Got password : com.fniephaus.gmail:gmail_credentials
Traceback (most recent call last):
  File "gmail.py", line 271, in <module>
    main()
  File "gmail.py", line 133, in main
    refresh_cache([label])
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/gmail_refresh.py", line 122, in refresh_cache
    gmail_service = build('gmail', 'v1', http=http)
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/apiclient/discovery.py", line 198, in build
    raise HttpError(resp, content, uri=requested_url)
apiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest returned "This service is not available from Iran. If you believe the country of origin was incorrectly identified, please report it at https://support.google.com/websearch/contact/ip.">

That's unfortunate to say that Google blocks access to its Gmail API from Iranian IPs. So, basically, everyone needs to use some kinda VPN or proxy to access it here.

That'd be great (and much appreciated) the workflow could detect and use the system's set proxy.

system socks proxy

fniephaus commented 7 years ago

Hi @sepehr, Thanks for reporting this. I was assuming that Python would already automatically respect your proxy settings. I will look into a solution for this...

fniephaus commented 7 years ago

Hi @sepehr, So it looks like that the Google Python client that I bundled with the workflow didn't support SOCKS proxies. Could you please download and try out the following updated version of the workflow: Gmail-Client-for-Alfred.alfredworkflow.zip. Let me know if that works for you, otherwise I need to dig deeper.

sepehr commented 7 years ago

Hey @fniephaus,

Thank you so much for looking into this 🙏

I just installed it, now it does not even list the primary actions. Here's the debug log:

[2017-04-19 17:07:35][ERROR: input.scriptfilter] Code 1: Traceback (most recent call last):
  File "gmail.py", line 2, in <module>
    from gmail_refresh import refresh_cache
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/gmail_refresh.py", line 10, in <module>
    from oauth2client.tools import run
ImportError: cannot import name run

I fixed the exception in the gmail_refresh.py:

from oauth2client.tools import run_flow as run

Now, it shows me a "No mails found!" message while there is:

screen shot 2017-04-19 at 5 11 57 pm

Here's the debug log with the updated python file:

[2017-04-19 17:12:39][STDERR: input.scriptfilter] 17:12:38 workflow.py:1628 DEBUG    Reading settings from `/Users/Sepehr/Library/Application Support/Alfred 3/Workflow Data/com.fniephaus.gmail/settings.json` ...
17:12:38 workflow.py:2394 DEBUG    Last run version : 1.2.0
17:12:38 workflow.py:2394 DEBUG    Last run version : 1.2.0
17:12:38 workflow.py:2444 DEBUG    update_data : None
17:12:38 workflow.py:1859 DEBUG    Loading cached data from : /Users/Sepehr/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/com.fniephaus.gmail/gmail_inbox.cpickle
[2017-04-19 17:12:39][input.scriptfilter] <?xml version="1.0" encoding="utf-8"?>
<items><item autocomplete="" valid="no"><title>No mails found!</title><subtitle /><icon>icons/alert-dark.png</icon></item><item autocomplete="" valid="no"><title>...</title><subtitle /><icon>icons/mail-reply-dark.png</icon></item></items>
fniephaus commented 7 years ago

Whoops, thanks @sepehr. I still had the old .pyc on my filesystem for some reason, but I'll update gmail_refresh.py with your fix the next time I push to the repo. Well, we are not sure if the proxy issue really is gone until you see your emails. Do you have Python scripts that work with your SOCKS proxy settings? httplib2, which Google's Python API client uses, also comes with SocksiPy, a Python SOCKS module. So I hope this respects your SOCKS settings. Let me know if you find out more...

sepehr commented 7 years ago

Here's the situation with the new workflow:

So, I'm sure there's a problem with the new workflow. Is it working for you?

P. S. No, unfortunately, I don't have any existing scripts to test this out. But I look into it.

fniephaus commented 7 years ago

It is working for me. Maybe reauthenticate the workflow and try again. You can start from scratch after typing in gmail wf:deauth which deletes your access token.

sepehr commented 7 years ago

Re-authed the application, still the "No mails found!" message... everywhere.

No mails found! everywhere

fniephaus commented 7 years ago

Maybe have a look at the gmail_refresh.py and see how the client works. Then, try to build a simple script that for example fetches entries from your inbox and prints them to stdout. Let me know if you need help. I have the slight feeling that there is something wrong with your setup or the Python client...

sepehr commented 7 years ago

Thank you @fniephaus. Will do.

Would you please gimme a diff of what you've done with the python script to support the socks proxy?

fniephaus commented 7 years ago

No worries, @sepehr. I haven't really done anything but update all dependencies including httplib2 and the Google Python client. Maybe start with the client first and then try to get one of the examples working.

sepehr commented 7 years ago

So, basically its the current HEAD? right?

fniephaus commented 7 years ago

Yes, that's correct.

sepehr commented 7 years ago

I'm pretty sure that the recent commits have introduced a new bug. I ran through the history with git bisect and found out that since 5b9e3ba, the workflow is returning that "No mails found!" message no matter what. The latest working snapshot for me is 344b42c.

And I think I found the issue. I'll open a pull request to address it. Please see #19:

With this fix, now I've returned to that "This service is not available from Iran" error when using the socks proxy. I'll look into it as soon as I can find some time.

fniephaus commented 7 years ago

Thanks!