How to perform OAuth 2.0 using the Curl CLI? - Stack Overflow (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (Stack Overflow) (https://stackoverflow.com/opensearch.xml) (https://stackoverflow.com/questions/53357741/how-to-perform-oauth-2-0-using-the-curl-cli) (Feed for question 'How to perform OAuth 2.0 using the Curl CLI?') (https://stackoverflow.com/feeds/question/53357741) (site logo) Join Stack Overflow By clicking “Sign up”, you agree to our (https://stackoverflow.com/legal/terms-of-service/public) terms of service and acknowledge you have read our (https://stackoverflow.com/legal/privacy-policy) privacy policy . Sign up with Google Sign up with GitHub OR Email Password (8+ characters (at least 1 letter & 1 number)) Sign up Already have an account? (https://stackoverflow.com/users/login) Log in Skip to main content (https://stackoverflow.com/) Stack Overflow (https://stackoverflow.co/) About Products (https://stackoverflow.co/teams/ai/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav-bar&utm_content=overflowai) OverflowAI (Search…) (Click to show search) (https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f53357741%2fhow-to-perform-oauth-2-0-using-the-curl-cli) Log in (https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f53357741%2fhow-to-perform-oauth-2-0-using-the-curl-cli) Sign up Let's set up your homepage Select a few topics you're interested in: python javascript c# reactjs java android html flutter c++ node.js typescript css r php angular next.js spring-boot machine-learning sql excel ios azure docker Or search from our full list: (Search) Next You’ll be prompted to create an account to view your personalized homepage. (https://stackoverflow.com/) Home (https://stackoverflow.com/questions) Questions (https://stackoverflow.com/tags) Tags (https://stackoverflow.com/beta/discussions) Discussions Labs (https://chat.stackoverflow.com/?tab=all&sort=active) Chat (https://stackoverflow.com/users) Users (https://stackoverflow.com/jobs?source=so-left-nav) Jobs (https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav) Companies Collectives Communities for your favorite technologies. (https://stackoverflow.com/collectives-all) Explore all Collectives Teams Ask questions, find answers and collaborate at work with Stack Overflow for Teams. (https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) Try Teams for free (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) Explore Teams (Illustration of upvote icon after it is clicked) Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. Upvoting indicates when questions and answers are useful. (https://stackoverflow.com/help/whats-reputation) What's reputation and how do I get it? Instead, you can save this post to reference later. Save this post for later Not now (Illustration of upvote icon after it is clicked) Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. Upvoting indicates when questions and answers are useful. (https://stackoverflow.com/help/whats-reputation) What's reputation and how do I get it? To start gaining reputation, try answering a related question. curl oauth-2.0 google-cloud-platform google-oauth Find related question Not now (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (https://stackoverflow.com/questions/53357741/how-to-perform-oauth-2-0-using-the-curl-cli) How to perform OAuth 2.0 using the Curl CLI? (https://stackoverflow.com/questions/ask) Ask Question (2018-11-18 03:53:33Z) Asked 6 years, 5 months ago Modified (https://stackoverflow.com/questions/53357741/how-to-perform-oauth-2-0-using-the-curl-cli?lastactivity) (2020-03-20 20:44:34Z) 5 years, 1 month ago (Viewed 94,732 times) Viewed 95k times Part of (https://stackoverflow.com/collectives/google-cloud) Google Cloud Collective This question shows research effort; it is useful and clear 40 (This question does not show any research effort; it is unclear or not useful) Save this question. (https://stackoverflow.com/posts/53357741/timeline) Show activity on this post. I would like to use curl from a Windows command prompt to perform Google OAuth 2.0. My goal is to better understand the authentication flows that an OAuth server implements, see the HTTP headers, etc. How can this be done using curl.exe from a Windows Command Prompt? (https://stackoverflow.com/collectives/google-cloud) Google Cloud Collective (https://stackoverflow.com/questions/tagged/curl) (show questions tagged 'curl') curl (https://stackoverflow.com/questions/tagged/oauth-2.0) (show questions tagged 'oauth-2.0') oauth-2.0 (https://stackoverflow.com/questions/tagged/google-cloud-platform) (show questions tagged 'google-cloud-platform') google-cloud-platform (https://stackoverflow.com/questions/tagged/google-oauth) (show questions tagged 'google-oauth') google-oauth (https://stackoverflow.com/q/53357741) (Short permalink to this question) Share (https://stackoverflow.com/posts/53357741/edit) Improve this question Follow Follow this question to receive notifications asked (2018-11-18 03:53:33Z) Nov 18, 2018 at 3:53 (https://stackoverflow.com/users/8016720/john-hanley) (John Hanley's user avatar) (https://stackoverflow.com/users/8016720/john-hanley) John Hanley John Hanley (reputation score 81,560) 81.6k (7 gold badges) 7 7 gold badges (113 silver badges) 113 113 silver badges (179 bronze badges) 179 179 bronze badges (Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.) Add a comment | (Expand to show all comments on this post) 1 Answer 1 Sorted by: (https://stackoverflow.com/questions/53357741/how-to-perform-oauth-2-0-using-the-curl-cli?answertab=scoredesc#tab-top) Reset to default (scoredesc) Highest score (default) (trending) Trending (recent votes count more) (modifieddesc) Date modified (newest first) (createdasc) Date created (oldest first) This answer is useful 34 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/53357742/timeline) Show activity on this post. How to perform OAuth 2.0 using the Curl CLI? This answer is for Windows Command Prompt users but should be easily adaptable to Linux and Mac also. You will need your Google Client ID and Client Secret . These can be obtained from the Google Console under APIs & Services -> Credentials . In the following example, the Scope is cloud-platform . Modify to use the scopes that you want to test with. Here are a few scopes that you can test with: "https://www.googleapis.com/auth/cloud-platform" "https://www.googleapis.com/auth/cloud-platform.read-only" "https://www.googleapis.com/auth/devstorage.full_control" "https://www.googleapis.com/auth/devstorage.read_write" "https://www.googleapis.com/auth/devstorage.read_only" "https://www.googleapis.com/auth/bigquery" "https://www.googleapis.com/auth/datastore" (https://developers.google.com/identity/protocols/googlescopes) OAuth 2.0 Scopes for Google APIs Details: Copy the following statements to a Windows batch file. Modify to fit your environment. Modify the script for the browser that you want to use. Run the batch file. A browser will be launched. The browser will go to (https://accounts.google.com/) https://accounts.google.com where you can complete the Google OAuth 2.0 authentication. Once complete a code will be displayed in the browser window. Copy this code (control-c) from the browser window and paste into the command prompt window (control-rightclick). The script will complete the OAuth 2.0 code exchange for a Token. The Token will be displayed in the command prompt. The returned Token contains an Access Token that can be used in more curl commands. Windows Batch Script: set CLIENT_ID=Replace_with_your_Client_ID set CLIENT_SECRET=Replace_with_your_Client_Secret set SCOPE=https://www.googleapis.com/auth/cloud-platform set ENDPOINT=https://accounts.google.com/o/oauth2/v2/auth set URL="%ENDPOINT%?client_id=%CLIENT_ID%&response_type=code&scope=%SCOPE%&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob" @REM start iexplore %URL% @REM start microsoft-edge:%URL% start chrome %URL% set /p AUTH_CODE="Enter Code displayed in browser: " curl ^ --data client_id=%CLIENT_ID% ^ --data client_secret=%CLIENT_SECRET% ^ --data code=%AUTH_CODE% ^ --data redirect_uri=urn:ietf:wg:oauth:2.0:oob ^ --data grant_type=authorization_code ^ https://www.googleapis.com/oauth2/v4/token The final output looks like this: { "access_token": "ya29.deleted_for_security_reasons", "expires_in": 3600, "refresh_token": "1/jk3/deleted_for_security_reasons", "scope": "https://www.googleapis.com/auth/cloud-platform", "token_type": "Bearer" } Example curl command using Access Token: set ACCESS_TOKEN=replace_with_your_access_token set PROJECT=development-123456 set ZONE=us-west-1a set INSTANCE_NAME=dev-system @REM - This endpoint will start the instance named INSTANCE_NAME in ZONE set ENDPOINT=https://www.googleapis.com/compute/v1/projects/%PROJECT%/zones/%ZONE%/instances/%INSTANCE_NAM%/start curl -H "Authorization: Bearer %ACCESS_TOKEN" "%ENDPOINT%" Tip: Save the Access Token to a file Modify the last line of the batch script to use jq to process the output: curl ^ --data client_id=%CLIENT_ID% ^ --data client_secret=%CLIENT_SECRET% ^ --data code=%AUTH_CODE% ^ --data redirect_uri=urn:ietf:wg:oauth:2.0:oob ^ --data grant_type=authorization_code ^ https://www.googleapis.com/oauth2/v4/token | jq -r ".access_token > token.save set /p ACCESS_TOKEN=