• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by RudyDev · Sep 06, 2016 at 04:44 AM · jsongoogle playgoogleauthenticationtoken

Making a POST request to Google OAuth using WWWForms

Hi, I'm trying to write a WWWForm to make a HTTP POST request to exchange an auth code for an access token. This is my first time working with WWWForms and I would like a bit of guidance.

Google gives an example of the request:

 POST /oauth2/v4/token HTTP/1.1
 Host: www.googleapis.com
 Content-Type: application/x-www-form-urlencoded
 
 code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
 client_id=8819981768.apps.googleusercontent.com&
 client_secret=your_client_secret&
 redirect_uri=https://oauth2.example.com/code&
 grant_type=authorization_code

I retrieve my auth code using GetServerAuthCode(), and pass that to this function:

         public IEnumerator GetGoogleAccessToken(string auth_code) {
             WWWForm form = new WWWForm();
     
             Dictionary<string, string> headers = new Dictionary<string, string>();
    
             headers.Add("Host", "www.googleapis.com");
             headers.Add("Content-Type", "application/x-www-form-urlencoded");
             form.AddField("code", auth_code + "&");
             form.AddField("client_id", client_id);
             form.AddField("client_secret", client_secret);
             form.AddField ("redirect_uri", redirect_uri);
             form.AddField("grant_type", "authorization_code");
             byte[] rawData = form.data;
 
             WWW www = new WWW(token_uri, rawData, headers);
             yield return www;
         }

  1. Am I suppose to append '&' to the end of all my strings?

  2. Referring to this, section 4: https://developers.google.com/identity/protocols/OpenIDConnect I believe that my endpoint url should be https://www.googleapis.com/oauth2/v4/token ? Does that seem correct?

  3. Why is it that when I do not modify headers, I can call WWW(url, form), but when I do, I have to call WWW(url, rawData, headers) ? In other words, how does sending a form differ from sending a byte array?

  4. I'm expecting JSON data to be return. Any examples or suggestions on how I should parse that in C#?

Thank you!

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by RudyDev · Sep 12, 2016 at 01:09 AM

I managed to get a response, albeit an error:

 {
    "error": "invalid_client",
    "error_description": "The OAuth client was not found."
 }

I'm using a Web Application Client ID generated from the Google Developer Console.

Is it possible that I need to encode my URL in some way? I'm simply passing it as a string via client_id

Googled this issue, people report changing the Product name to equal the Project name (In Credentials) will solve this. I have them both the same, but the issue persists.

An feedback would help, thanks!

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AshyB · Apr 15, 2017 at 10:43 AM 0
Share

Did you ever figure this out?

avatar image

Answer by Schaken06 · Oct 12, 2022 at 04:05 PM

Not sure you still need this or not.

 private IEnumerator GetGoogleAccessToken(string TempKey) {
     Dictionary<string, string> content = new Dictionary<string, string>();
     content.Add("grant_type", "authorization_code");
     content.Add("code", TempKey);
     content.Add("redirect_uri", "REDIRECT_URI");
     content.Add("client_id", "CLIENT_ID");
     content.Add("client_secret", "CLIENT_SECRET");

     UnityWebRequest www = UnityWebRequest.Post("https://oauth2.googleapis.com/token", content);
     yield return www.SendWebRequest();
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable to authenticate using Firebase Auth 0 Answers

accessToken in Firebase Auth 2 Answers

How can I remove Google Play Services ?,How to remove Google Play Services ? 1 Answer

How can I Get AWS Cognito id token From Google Login? 0 Answers

Chromecast plugin for Unity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges