I think has found cause of the error 500. The error occurs when clients and servers are located in different geographical regions. I'm in Thailand, but the server is located in Germany. I click the link and confirm access, then I'm redirected to my server in Germany (redirect_uri). The server successfully receives an authorization code, but the request token ends with HTTP error 500 in more than 90% of cases.
I checked the IP address for
accounts.google.com. Thailand,
accounts.google.com = 209.85.175.84. Germany
accounts.google.com = 209.85.148.84. I added the following entry "209.85.175.84
accounts.google.com" in the file /etc/hosts on the server in Germany and did not receive any HTTP 500 error. After removal of this entry, I again got errors.
This means that there is problems synchronization
accounts.google.com in different regions. I found a lot of unresolved topics at
groups.google.com related to this issue.