asssaf / gmail-relay

Simple MTA that uses gmail API using a client OAuth token to send mail
MIT License
0 stars 0 forks source link

TypeError: is not JSON serializable #1

Open TeddJohnson opened 7 years ago

TeddJohnson commented 7 years ago

Hey there,

I've been playing around with this, but haven't been able to get anything to send. I got the authentication part done, but am having issues with the actual message. my "test" file looks like this:

(user@gmail.com is substituted for the actual username that was authenticated) x-sender: user@gmail.com x-receiver: myotheremail@gmail.com from: MyName user@gmail.com to: You myotheremail@gmail.com Subject: Testing Subject Hey, did you know that this works? .

The error I'm getting: (venv) [root@localhost venv]# cat ../test | ./gmail-relay.py Traceback (most recent call last): File "./gmail-relay.py", line 117, in <module> main() File "./gmail-relay.py", line 110, in main sent = send_message(service, "me", message) File "./gmail-relay.py", line 68, in send_message message = (service.users().messages().send(userId=user_id, body=message) File "/root/venv/lib/python3.4/site-packages/apiclient/discovery.py", line 642, in method actual_path_params, actual_query_params, body_value) File "/root/venv/lib/python3.4/site-packages/apiclient/model.py", line 149, in request body_value = self.serialize(body_value) File "/root/venv/lib/python3.4/site-packages/apiclient/model.py", line 258, in serialize return simplejson.dumps(body_value) File "/usr/lib64/python3.4/json/__init__.py", line 230, in dumps return _default_encoder.encode(obj) File "/usr/lib64/python3.4/json/encoder.py", line 192, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib64/python3.4/json/encoder.py", line 250, in iterencode return _iterencode(o, 0) File "/usr/lib64/python3.4/json/encoder.py", line 173, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: b'eC1zZW5kZXI6IHRlZGRqb2huc29uQGdtYWlsLmNvbQp4LXJlY2VpdmVyOiB0ZWRkam9obnNvbjk5QHlhaG9vLmNvbQpGcm9tOiBNeXNlbGYgUGVyc29uYWxseSA8dGVkZGpvaG5zb25AZ21haWwuY29tPgpUbzogWW91IDx0ZWRkam9obnNvbjk5QHlhaG9vLmNvbT4KU3ViamVjdDogSSBsb3ZlIGl0CkhleSwgZGlkIHlvdSBrbm93IHRoYXQKLgo=' is not JSON serializable

I'm thinking the issue is either because my test input file is not formatted correctly (RFC 2822) or maybe because I'm using python 3 instead of 2?

Any help would be appreciated, thank you

asssaf commented 7 years ago

I tried running it with python3 and it didn't get very far:

File "/usr/bin/gmail-relay.py", line 76
    except errors.HttpError, error:
                           ^ 
SyntaxError: invalid syntax

Running your test email with python2 gives me a format error:

An error occurred: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Invalid to header">

Which i fixed by adding carets around the email address (possibly it got cut out by github when you posted the comment). Then it works.

I'll look into making this python3 compatible, i never tried that before. In the meanwhile, could you try with python2?

TeddJohnson commented 7 years ago

Thanks for your quick response!

It looks like Github did remove my carrots. that means the format is correct.

For the part you're getting an error on, all you have to do is: except (errors.HttpError) as error: print('An error occurred:', error)

I will try with Python2

Thanks!

TeddJohnson commented 7 years ago

Python2 worked just fine it appears.

(venv2) [root@localhost venv2]# cat ../test | ./gmail-relay.py Message Id: 15b11fae26623567

unfortunately I got a bounce message from gmail, with absolutely no help indicating why, but that's an entirely different issue.

Hopefully we can figure out the python 3 issues. Thanks!

asssaf commented 7 years ago

I'm also getting the bounce message (in addition to the actual message). This is new, I'm not sure why it's happening.

TeddJohnson commented 7 years ago

thanks for the update. Then at least I'm not doing something totally wrong here. Could it be the way the test message is formatted? do you have an example of one that worked before?

thanks!

asssaf commented 7 years ago

This one will not create the bounce message:

Received: (nullmailer pid 10935 invoked by uid 1000);
    Mon, 27 Mar 2017 23:16:14 -0000
To: user@gmail.com
From: root@toecutter.toecutter, <user@gmail.com>
Subject: Service status - testtest
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8
Date: Mon, 27 Mar 2017 16:16:14 -0700
Message-Id: <1490656574.015347.10933.nullmailer@toecutter>

● testtest.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)
asssaf commented 7 years ago

@JawnZ did you ever get it to work?

BTW, would changing the script header to use python2 be a good solution, or do you think it needs to support both python2 and python3?