Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by turbolek · Jan 05, 2020 at 09:18 PM · googlewebrequesthttphttpwebrequestrest

Why do I get Parse error (code 400) from Google Ftiness API in Unity?

I am trying to read steps count from GoogleFitness API in Unity application. First I go to Google's OAuthPlayground, authorize the fitness api and copy the access token. In Unity I have a scene with text field and a button. I paste the access token to the textfield and click the button. On Button click the method GetStepsResponse is being called with input field's content as a param.

The logic behind:

     public void GetStepsResponse(string idToken)
 {
 
     StartCoroutine(SendPostCoroutine(idToken));
 }
 
 IEnumerator SendPostCoroutine(string accessToken)
 {
     string request = "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate";
     request = string.Concat(request, "?access_token=", accessToken);
     string body = "{  \"aggregateBy\": [{    \"dataTypeName\": \"com.google.step_count.delta\",    \"dataSourceId\": \"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps\"  }],  \"bucketByTime\": { \"durationMillis\": 86400000 }, // This is 24 hours  \"startTimeMillis\": 1577059200000, //start time  \"endTimeMillis\": 1577577600000// End Time}";
 
     UnityWebRequest webRequest = UnityWebRequest.Post(request, body);
     webRequest.SetRequestHeader("Content-type", "application/json");
 
     yield return webRequest.SendWebRequest();
 
     Debug.Log(webRequest.downloadHandler.text);
 }

as a response I get:

 {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
 }

I tried the same request with same body and headers here: https://apitester.com/ and I got a proper response with steps count for every day from given range. What I am missing?

EDIT

I came up with an idea that escaping the quatiion mark in the request body may cause the Parse Error, so I switched from manually constructing a request body string to serializing an object to JSON and using the product string as a body:

 public void GetStepsResponse(string idToken)
     {
 
         StartCoroutine(SendPostCoroutine(idToken));
     }
     IEnumerator SendPostCoroutine(string accessToken)
     {
         string request = "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate";
         request = string.Concat(request, "?access_token=", accessToken);
 
         FitnessRequest requestBody = new FitnessRequest();
         requestBody.aggregateBy = new AggregateByRequestParam[1];
         requestBody.aggregateBy[0] = new AggregateByRequestParam();
         requestBody.aggregateBy[0].dataTypeName = "com.google.step_count.delta";
         requestBody.aggregateBy[0].dataSourceId = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps";
         requestBody.bucketByTime = new DurationRequestParam();
         requestBody.bucketByTime.durationMillis = 86400000;
         requestBody.startTimeMillis = 1577059200000;
         requestBody.endTimeMillis = 1577577600000;
 
         string body = JsonUtility.ToJson(requestBody);
         Debug.Log(body);
 
         UnityWebRequest webRequest = UnityWebRequest.Post(request, body);
         webRequest.SetRequestHeader("Content-type", "application/json");
 
         yield return webRequest.SendWebRequest();
 
         Debug.Log(webRequest.downloadHandler.text);
     }
 
     [System.Serializable]
     private class FitnessRequest
     {
         public AggregateByRequestParam[] aggregateBy;
         public DurationRequestParam bucketByTime;
         public long startTimeMillis;
         public long endTimeMillis;
     }
 
     [System.Serializable]
     private class AggregateByRequestParam
     {
         public string dataTypeName;
         public string dataSourceId;
     }
 
     [System.Serializable]
     private class DurationRequestParam
     {
         public long durationMillis;
     }

However the result is still the same. Again, when I use the same request url and request body outside Unity it works (I get code 200 as a response). Is there something specific in Unity's UnityWebRequest.Post that I'm missing?

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

118 People are following this question.

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

Related Questions

Try catch alternative for HTTP request 1 Answer

HTTP PUT in Unity for iOS? 2 Answers

Get data from REST works on Unity but not on Android Device 1 Answer

Unity HTTP Post WebRquest not working on some Android Devices 0 Answers

UnityWebRequest object downloadhandler.text not returning same/all data compared to browser 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