0

I've been having a very difficult time sending a simple message to Glass and I suspect it has something to do with authorization request. Below is my code:

String AUTH="ya29.1.AADtN_WhePFbTxZdrM8wDWaVyBPgpyvph5EifU-CDQ4z9nbJxBXHLQLiaY3EDBk";

          for (int i=5; i>0; i--)
          {
                  String url = "https://www.googleapis.com/mirror/v1/timeline?fields=text";
                  URL obj = new URL(url);
                  HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

                  con.setRequestMethod("POST");
                  con.setRequestProperty("X-JavaScript-User-Agent", "Google APIs Explorer");
                  con.setRequestProperty("Authorization", "Bearer " + AUTH);
                  con.setRequestProperty("Content-Type", "application/json");
                  con.setRequestProperty("Content-Length", "26");
                  String urlParameters = "{ \"text\": \"Hello world\" }";

Unfortunately, it keeps throwing a 403 error. Any advice? Thanks.

Kara
  • 5,650
  • 15
  • 48
  • 55

1 Answers1

1

For starters, you shouldn't post an auth key to StackOverflow.

The problem is, likely, that the auth key has expired. Keys are only valid for 60 minutes, by default, and there are many reasons that they may end up being shorter than that. If you're using the API Explorer, it will generate and renew keys for you, but won't tell you what they are to prevent you from running into this exact problem. (Make sure, also, that the client app that this token was issued for has the Mirror API turned on in the API Console or Cloud Console.)

If you're using Java (which it appears you are), you should probably use the OAuth client libraries to manage the authentication for you and the Mirror library to make the calls. These libraries will attempt to do the call and, if the token is invalid, will re-auth and re-run the query. You can find out more about both at https://developers.google.com/api-client-library/java/apis/

If you insist on rolling the calls yourself (and it is certainly a good educational experience), make sure you catch the 403 error and use the refresh token (you did ask for a refresh token, right?) to get a new auth token.

Prisoner
  • 48,391
  • 6
  • 48
  • 97