问题
I have a web app hosted on IIS 7 that is doing Http calls using the Google Fit API, I'm able to successfully send a POST and retrieve an access token, after which I do a GET for the following uri:
"https://www.googleapis.com/fitness/v1/users/me/dataSources/raw:com.google.weight:com.google.android.apps.fitness:user_input/datasets/00-1427209862000000000"
Here's how I build a request and look at the response:
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.ContentLength = 0;
request.Accept = "application/json";
request.Headers.Add("Authorization", "Bearer " + dict["access_token"]);
response = (HttpWebResponse)request.GetResponse();
respStream = response.GetResponseStream();
sResponse = new StreamReader(respStream).ReadToEnd();
respStream.Close();
Response.Write(sResponse);
When I run this app on a browser on the host server, I successfully get a json object (it isn't the json I expect, but that's another issue). However, when I try to access the site on a remote client, I get a 403 error pointing to when I try to retrieve the response. Any ideas?
回答1:
It probably depends how you got the access_token
value.
How long after retrieving the access token are you making this request? It's only valid for an hour, so you may need to fetch a new one using the refresh token.
There's some more resources on access/refresh tokens this question:
Google OAuth2 Refresh_token expires when Access_token does
回答2:
Update: This turned out to be a permissions issue. I was trying to access a data source that I didn't have scopes for, the fact that it didn't return a 403 when accessing on the host threw me off the trail. Still strange that it didn't return a 403 (it just returned an empty json object) when requested on the host server though...if you see this and have an idea why, feel free to comment. I'm curious.
来源:https://stackoverflow.com/questions/29281957/google-fit-api-403-error-from-remote-client