{"action":"created","issue":{"url":"https://api.github.com/repos/google/google-auth-library-nodejs/issues/379","repository_url":"https://api.github.com/repos/google/google-auth-library-nodejs","labels_url":"https://api.github.com/repos/google/google-auth-library-nodejs/issues/379/labels{/name}","comments_url":"https://api.github.com/repos/google/google-auth-library-nodejs/issues/379/comments","events_url":"https://api.github.com/repos/google/google-auth-library-nodejs/issues/379/events","html_url":"https://github.com/google/google-auth-library-nodejs/issues/379","id":329968252,"node_id":"MDU6SXNzdWUzMjk5NjgyNTI=","number":379,"title":"OAuth exchange giving invalid_grant and Bad request","user":{"login":"Fleker","id":3291635,"node_id":"MDQ6VXNlcjMyOTE2MzU=","avatar_url":"https://avatars2.githubusercontent.com/u/3291635?v=4","gravatar_id":"","url":"https://api.github.com/users/Fleker","html_url":"https://github.com/Fleker","followers_url":"https://api.github.com/users/Fleker/followers","following_url":"https://api.github.com/users/Fleker/following{/other_user}","gists_url":"https://api.github.com/users/Fleker/gists{/gist_id}","starred_url":"https://api.github.com/users/Fleker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Fleker/subscriptions","organizations_url":"https://api.github.com/users/Fleker/orgs","repos_url":"https://api.github.com/users/Fleker/repos","events_url":"https://api.github.com/users/Fleker/events{/privacy}","received_events_url":"https://api.github.com/users/Fleker/received_events","type":"User","site_admin":false},"labels":[{"id":944195306,"node_id":"MDU6TGFiZWw5NDQxOTUzMDY=","url":"https://api.github.com/repos/google/google-auth-library-nodejs/labels/priority:%20p2","name":"priority: p2","color":"fef2c0","default":false},{"id":176254514,"node_id":"MDU6TGFiZWwxNzYyNTQ1MTQ=","url":"https://api.github.com/repos/google/google-auth-library-nodejs/labels/type:%20bug","name":"type: bug","color":"db4437","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-06-06T17:46:00Z","updated_at":"2018-06-07T17:55:31Z","closed_at":"2018-06-07T01:10:18Z","author_association":"NONE","body":"I added [a question on StackOverflow](https://stackoverflow.com/questions/50709360/oauth-exchange-giving-invalid-grant-and-bad-request) but I'd also like to bring it up here since it may be an issue with the library as it works fine with curl.\r\n\r\n===\r\n\r\nI'm running into some weird behavior when trying to use OAuth for a local Node.js script. My goal for right now is to obtain a refresh token from the user when they authorize my project.\r\n\r\nI'm trying to rely as much as I can on Google's libraries but am still having issues with it.\r\n\r\nWhen I try to run the setup, I correctly get the OAuth page and create an authorization token:\r\n\r\n```js\r\n    const { google } = require('googleapis');\r\n\r\n    const oauth2Client = new google.auth.OAuth2(\r\n        clientId,\r\n        clientSecret,\r\n        'urn:ietf:wg:oauth:2.0:oob'\r\n    );\r\n    const scopes = [\r\n        'https://www.googleapis.com/auth/calendar'\r\n    ];\r\n```\r\n\r\nI provide the URL to the user in the console:\r\n\r\n```js\r\n    const url = oauth2Client.generateAuthUrl({\r\n        // 'online' (default) or 'offline' (gets refresh_token)\r\n        access_type: 'offline',\r\n\r\n        // If you only need one scope you can pass it as a string\r\n        scope: scopes\r\n    });\r\n\r\n    console.log(url);\r\n```\r\n\r\nThen I open the URL and generate the token. I prompt the user to provide this value:\r\n\r\n```js\r\n    stdio.question('Authorization code', (err, code) => {\r\n        // Use this code to obtain a refresh token\r\n        console.log(\"CODE\", code);\r\n        oauth2Client.getToken(code, (err, tokens) => {\r\n            console.log(err, tokens);\r\n        });\r\n    });\r\n```\r\n\r\nWhen I get to this point, I run into an error:\r\n\r\n    error: 'invalid_grant', error_description: 'Bad Request'\r\n\r\nAnd it's not clear where this is going wrong.\r\n\r\nIf I take the same auth token and use cURL, it actually works as expected, so it's not a configuration error as far as I can tell.\r\n\r\n    curl -s -X POST -d 'code='$CODE'&client_id='$CLIENT_ID'&client_secret='$CLIENT_SECRET'&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code' https://www.googleapis.com/oauth2/v4/token\r\n\r\n```js\r\n    {\r\n      \"access_token\": \"ACCESS\",\r\n      \"token_type\": \"Bearer\",\r\n      \"expires_in\": 3600,\r\n      \"refresh_token\": \"REFRESH\"\r\n    }\r\n```\r\n\r\nWhat's even more interesting is that if I run through the flow and get the error, trying to run the cURL command will return an error that indicates the request was accepted to some degree:\r\n\r\n```js\r\n    {\r\n      \"error\": \"invalid_grant\",\r\n      \"error_description\": \"Code was already redeemed.\"\r\n    }\r\n```\r\n\r\nWhat is happening in this OAuth library that is causing the invalid grant issue that is not happening with cURL, and what can I do to correct the Node.js code?"},"comment":{"url":"https://api.github.com/repos/google/google-auth-library-nodejs/issues/comments/395509728","html_url":"https://github.com/google/google-auth-library-nodejs/issues/379#issuecomment-395509728","issue_url":"https://api.github.com/repos/google/google-auth-library-nodejs/issues/379","id":395509728,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NTUwOTcyOA==","user":{"login":"Fleker","id":3291635,"node_id":"MDQ6VXNlcjMyOTE2MzU=","avatar_url":"https://avatars2.githubusercontent.com/u/3291635?v=4","gravatar_id":"","url":"https://api.github.com/users/Fleker","html_url":"https://github.com/Fleker","followers_url":"https://api.github.com/users/Fleker/followers","following_url":"https://api.github.com/users/Fleker/following{/other_user}","gists_url":"https://api.github.com/users/Fleker/gists{/gist_id}","starred_url":"https://api.github.com/users/Fleker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Fleker/subscriptions","organizations_url":"https://api.github.com/users/Fleker/orgs","repos_url":"https://api.github.com/users/Fleker/repos","events_url":"https://api.github.com/users/Fleker/events{/privacy}","received_events_url":"https://api.github.com/users/Fleker/received_events","type":"User","site_admin":false},"created_at":"2018-06-07T17:55:31Z","updated_at":"2018-06-07T17:55:31Z","author_association":"NONE","body":"Thank you. I was really troubled by this issue and it does appear to be a longstanding issue in that library. I really appreciate you looking into this for me."}}
{"actor":{"display_login":"Fleker"}}