0

Hi guys I am trying to use scribe-java library to access the REST api via http.code looks

package org.scribe.examples;
import java.util.*;
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
public class WooCommerceOauth1Example {

private static final String RESOURCE_URL = "http://WEBSITE.COM/wc-api/v1/orders";

public static void main(String[] args) {
    OAuthService service = new ServiceBuilder().provider(OneLeggedApi10.class)
            .apiKey("ck_SOME_NUMBER")
            .apiSecret("cs_SOME_NUMBER")
            .build();


    // 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, RESOURCE_URL);
    //Since it is a one legged protocol, access token is empty.Right?      
    service.signRequest(new Token("", ""), 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());

    System.out.println();
    System.out.println("Thats it man! Go and build something awesome   with Scribe! :)");

   }
}

Throws the following error {"errors":[{"code":"woocommerce_api_authentication_error","message":"oauth_consumer_key parameter is missing"}]} . Any Ideas why my code is throwing the above error? Note that I have checked the v1 endpoint with http and it returns sensible message back.so basically it is working.

Misgevolution
  • 825
  • 10
  • 22

1 Answers1

2

Removing '&' + OAuthEncoder.encode(tokenSecret) from https://github.com/fernandezpablo85/scribe-java/blob/master/src/main/java/org/scribe/services/HMACSha1SignatureService.java#L32 and adding and changed signature type to QueryString and it works now.

I will propose a PR after cleaning.Thanks Pablo. Below is the full code

package org.scribe.builder.api;

import org.scribe.model.Token;
import org.scribe.model.Verb;

public class OneLeggedApi10 extends DefaultApi10a {

@Override
public String getAccessTokenEndpoint() {
    return null;
}

@Override
public String getRequestTokenEndpoint() {
    return null;
}

@Override
public String getAuthorizationUrl(Token requestToken) {
    return null;
}

@Override
public Verb getAccessTokenVerb() {
    return Verb.GET;
}

@Override
public Verb getRequestTokenVerb() {
    return Verb.GET;
}

}

And the example class

package org.scribe.examples;


import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;

 public class WooCommerceOauth1Example {

private static final String NETWORK_NAME = "Woocommerce";
private static final String RESOURCE_URL = "http://YOUR_DOMAIN/wc-api/v1/orders/count";
private static final String SCOPE = "*"; //all permissions

public static void main(String[] args) {
    OAuthService service = new ServiceBuilder().provider(OneLeggedApi10.class)
            .apiKey("API_KEY")                
            .apiSecret("SECRET_KEY") 
            .debugStream(System.out)
            .signatureType(SignatureType.QueryString)
            /*.scope(SCOPE).*/
            .build();

    System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
    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, RESOURCE_URL);

    service.signRequest(new Token("", ""), 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());

    System.out.println();
    System.out.println("Thats it man! Go and build something awesome with Scribe! :)");

}

}

Misgevolution
  • 825
  • 10
  • 22