Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for Withings #331

Closed
wants to merge 1 commit into from

3 participants

Eric Jain arwright Pablo Fernandez
Eric Jain

No description provided.

arwright

Wow. We were struggling and Eric's code here is exactly what we needed to fix our problem. Thanks Eric!

It looks like this pull request was accepted. I don't understand why these changes don't show up in the current version of scribe. We wouldn't have struggled as long if this were in the master tree instead of just tucked away in this pull request.

I don't understand why these changes don't show up in the current version of scribe

If you're talking about the Api, it should be on the source. If you're talking about the example, then it's not part of the library distribution (that's why it's on the "test" folder)

We wouldn't have struggled as long if this were in the master tree instead of just tucked away in this pull request.

I assume you read all the wikipages, specially the one that talks about custom apis (https://github.com/fernandezpablo85/scribe-java/wiki/Custom-Apis), right?

BTW I'm sorry to hear you struggled that long. That said, I try to reply to issues and emails within the hour and I never got one from you.

Hi Pablo,

Thanks for responding.

If you're talking about the Api, it should be on the source. If you're talking about the example, then it's not part of the library distribution (that's why it's on the "test" folder)

I assume you read all the wikipages, specially the one that talks about custom apis (https://github.com/fernandezpablo85/scribe-java/wiki/Custom-Apis), right?

We are actually using a local checkout of scribe-java which does contain the test folder and not a library distribution. We checked for Withings files in both the local checkout of src/main/java/org/scribe/builder/api and src/test/java/org/scribe/examples, as well as the corresponding api and test examples in the current head of the master branch on github. The relevant Withings support files (WithingsApi.java and WithingsExample.java) were not there.

We thought that this meant that Withings support had not yet been added to scribe-java. We created a local version, but were flailing trying to get it to work. Eric's WithingsExample.java file, once we stumbled upon it, provided the critical bit we were missing: the need to include .signatureType(SignatureType.QueryString) as part of creating the OAuthService.

Now that we know the right answer, I see that that call was indeed mentioned in the Custom-Apis page. We just didn't realize its relevance to the problems we were having with Withings until we saw it here in Eric's pull request.

BTW I'm sorry to hear you struggled that long. That said, I try to reply to issues and emails within the hour and I never got one from you.

I very much appreciate your responsiveness and willingness to help. We did not try to contact you on this issue because we felt that our difficulties reflected a problem with our understanding of how to apply scribe-java to an as-yet-unsupported service, not a problem with scribe-java itself. As it turns out, that feeling was correct. We just didn't know that there was already an example showing how to do it right was tucked away here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 1, 2012
  1. Eric Jain

    Add support for Withings

    ejain authored
This page is out of date. Refresh to see the latest.
36 src/main/java/org/scribe/builder/api/WithingsApi.java
View
@@ -0,0 +1,36 @@
+package org.scribe.builder.api;
+
+import org.scribe.model.Token;
+import org.scribe.model.Verb;
+
+/**
+ * OAuth API for Withings.
+ *
+ * @see <a href="http://www.withings.com/en/api/">Withings API</a>
+ */
+public class WithingsApi extends DefaultApi10a
+{
+
+ @Override
+ public String getRequestTokenEndpoint()
+ {
+ return "https://oauth.withings.com/account/request_token";
+ }
+
+ @Override
+ public Verb getRequestTokenVerb() {
+ return Verb.GET;
+ }
+
+ @Override
+ public String getAuthorizationUrl(Token requestToken)
+ {
+ return "https://oauth.withings.com/account/authorize?oauth_token=" + requestToken.getToken();
+ }
+
+ @Override
+ public String getAccessTokenEndpoint()
+ {
+ return "https://oauth.withings.com/account/access_token";
+ }
+}
71 src/test/java/org/scribe/examples/WithingsExample.java
View
@@ -0,0 +1,71 @@
+package org.scribe.examples;
+
+import java.util.Scanner;
+
+import org.scribe.builder.ServiceBuilder;
+import org.scribe.builder.api.WithingsApi;
+import org.scribe.model.OAuthRequest;
+import org.scribe.model.Response;
+import org.scribe.model.SignatureType;
+import org.scribe.model.Token;
+import org.scribe.model.Verb;
+import org.scribe.model.Verifier;
+import org.scribe.oauth.OAuthService;
+
+public class WithingsExample
+{
+ public static void main(String[] args)
+ {
+ // Replace these with your own api key and secret
+ String apiKey = "myKey";
+ String apiSecret = "mySecret";
+ OAuthService service = new ServiceBuilder()
+ .provider(WithingsApi.class)
+ .apiKey(apiKey).apiSecret(apiSecret)
+ .signatureType(SignatureType.QueryString)
+ .build();
+ Scanner in = new Scanner(System.in);
+
+ System.out.println("=== Withing's OAuth Workflow ===");
+ System.out.println();
+
+ // Obtain the Request Token
+ System.out.println("Fetching the Request Token...");
+ Token requestToken = service.getRequestToken();
+ System.out.println("Got the Request Token!");
+ System.out.println();
+
+ // Obtain the Authorization URL
+ System.out.println("Fetching the Authorization URL...");
+ String authorizationUrl = service.getAuthorizationUrl(requestToken);
+ System.out.println("Got the Authorization URL!");
+ System.out.println("Now go and authorize Withings here:");
+ System.out.println(authorizationUrl);
+ System.out.println("And paste the authorization code here");
+ System.out.print(">>");
+ Verifier verifier = new Verifier(in.nextLine());
+ System.out.println("And paste the user ID here");
+ System.out.print(">>");
+ String userId = in.nextLine();
+ System.out.println();
+
+ // Trade the Request Token and Verfier for the Access Token
+ System.out.println("Trading the Request Token for an Access Token...");
+ Token accessToken = service.getAccessToken(requestToken, verifier);
+ System.out.println("Got the Access Token!");
+ System.out.println("(if your curious it looks like this: " + accessToken + " )");
+ System.out.println();
+
+ // Now let's go and ask for a protected resource!
+ System.out.println("Now we're going to access a protected resource...");
+ OAuthRequest request = new OAuthRequest(Verb.GET, "http://wbsapi.withings.net/measure");
+ request.addQuerystringParameter("userid", userId);
+ request.addQuerystringParameter("action", "getmeas");
+ service.signRequest(accessToken, request);
+ Response response = request.send();
+ System.out.println("Got it! Lets see what we found...");
+ System.out.println();
+ System.out.println(response.getCode());
+ System.out.println(response.getBody());
+ }
+}
Something went wrong with that request. Please try again.