0

I am trying to upload video to vimeo and I understand that you need the ticket_id in order to be able to upload.

The think is I can not figure out how to get this ticket_id by using scribe. Does anyone have any example how to do this?

Thanks in advance.

When I use:

OAuthRequest request = new OAuthRequest(Verb.GET, "http://vimeo.com/api/rest/v2");
request.addQuerystringParameter("method", "vimeo.videos.upload.getTicket");

this results in:

<err code="401" expl="The consumer key passed was not valid." msg="Invalid consumer key"/>

When I use method:

request.addQuerystringParameter("method", "vimeo.videos.upload.getQuota");

everything works fine. I tried putting some fake api key in the vimeo.videos.upload.getQuota method. That also resulted in invalid key. So it is not like method vimeo.videos.upload.getQuota does not need the api key. In fact it does and if you don't provide the valid key it wil not work. Somehow when calling method vimeo.videos.upload.getTicket, with the same api key that works for the mehod getQuota, I get response:

<err code="401" expl="The consumer key passed was not valid." msg="Invalid consumer key"/>

full code with fake api keys:

public class VimeoServiceConcept {

public static void main(String[] args) {

    String apikey="api key";
    String apisecret="secret";
    String accessToken="access token";
    String accessTokenSecret="access token secret";



    OAuthService service = new ServiceBuilder()
    .provider(VimeoApi.class)
    .apiKey(apikey)
    .apiSecret(apisecret)
    .build();

    Token token = new Token(accessToken, accessTokenSecret);

    OAuthRequest request = new OAuthRequest(Verb.GET, "http://vimeo.com/api/rest/v2");
//  request.addQuerystringParameter("method", "vimeo.videos.upload.getQuota");
    request.addQuerystringParameter("format", "xml");
    request.addQuerystringParameter("method", "vimeo.videos.upload.getTicket");
    request.addQuerystringParameter("upload_method", "post");
    service.signRequest(token, request);        
    System.out.println(request.getCompleteUrl());
    Response response = request.send();

     System.out.println("Got it! Lets see what we found...");
     System.out.println(response.getHeader("code"));
     System.out.println(response.getCode());
     System.out.println(response.getBody());
   }
}
aki
  • 1,731
  • 2
  • 19
  • 24
  • @Krroae27 OAuthRequest request = new OAuthRequest(Verb.GET, "vimeo.com/api/rest/v2";); request.addQuerystringParameter("method", "vimeo.videos.upload.getTicket"); this results in: "The consumer key passed was not valid." – aki May 16 '12 at 11:16
  • http://developer.vimeo.com/apis/advanced/methods/vimeo.videos.upload.getTicket – Krrose27 May 16 '12 at 11:33
  • 1
    I've worked with scribe and the Upload part of the Vimeo API pretty extensively. To answer this question I need a bit more code. Could you give us a [SSCCE](http://www.sscce.org)? – kentcdodds May 16 '12 at 11:33
  • @kentcdodds I have added full code. – aki May 16 '12 at 11:48
  • @Krroae27 thanks but I already did that – aki May 16 '12 at 11:48

1 Answers1

2

Try getting the ticket after you get the quota. I have never tried getting the ticket without the quota first because their documentation explicitly states you need to check quota before you get the ticket. It looks like you just comment out what you're not testing. Try this instead:

public class VimeoServiceConcept {

public static void main(String[] args) {

    String apikey="api key";
    String apisecret="secret";
    String accessToken="access token";
    String accessTokenSecret="access token secret";

    OAuthService service = new ServiceBuilder().provider(VimeoApi.class).apiKey(apiKey).apiSecret(apiSecret).build();

    OAuthRequest request;
    Response response;

    accessToken = new Token("your_token", "your_tokens_secret");

    accessToken = checkToken(vimeoAPIURL, accessToken, service);
    if (accessToken == null) {
      return;
    }

    // Get Quota
    request = new OAuthRequest(Verb.GET, vimeoAPIURL);
    request.addQuerystringParameter("method", "vimeo.videos.upload.getQuota");
    signAndSendToVimeo(request, "getQuota", true);

    // Get Ticket
    request = new OAuthRequest(Verb.GET, vimeoAPIURL);
    request.addQuerystringParameter("method", "vimeo.videos.upload.getTicket");
    request.addQuerystringParameter("upload_method", "streaming");
    response = signAndSendToVimeo(request, "getTicket", true);
    //... the rest of your code...

   }
}

Here's checkToken:

/**
  * Checks the token to make sure it's still valid. If not, it pops up a dialog asking the user to
  * authenticate.
  */
  private static Token checkToken(String vimeoAPIURL, Token vimeoToken, OAuthService vimeoService) {
    if (vimeoToken == null) {
      vimeoToken = getNewToken(vimeoService);
    } else {
      OAuthRequest request = new OAuthRequest(Verb.GET, vimeoAPIURL);
      request.addQuerystringParameter("method", "vimeo.oauth.checkAccessToken");
      Response response = signAndSendToVimeo(request, "checkAccessToken", true);
      if (response.isSuccessful()
              && (response.getCode() != 200 || response.getBody().contains("<err code=\"302\"")
              || response.getBody().contains("<err code=\"401\""))) {
        vimeoToken = getNewToken(vimeoService);
      }
    }
    return vimeoToken;
  }

Here's getNewToken:

/**
* Gets authorization URL, pops up a dialog asking the user to authenticate with the url and the user
* returns the authorization code
*
* @param service
* @return
*/
private static Token getNewToken(OAuthService service) {
  // Obtain the Authorization URL
  Token requestToken = service.getRequestToken();
  String authorizationUrl = service.getAuthorizationUrl(requestToken);
  do {
    String code = JOptionPane.showInputDialog("The token for the account (whatever)" + newline
            + "is either not set or is no longer valid." + newline
            + "Please go to the URL below and authorize this application." + newline
            + "Paste the code you're given on top of the URL here and click \'OK\'" + newline
            + "(click the 'x' or input the letter 'q' to cancel." + newline
            + "If you input an invalid code, I'll keep popping up).", authorizationUrl + "&permission=delete");
    if (code == null) {
      return null;
    }
    Verifier verifier = new Verifier(code);
    // Trade the Request Token and Verfier for the Access Token
    System.out.println("Trading the Request Token for an Access Token...");
    try {
      Token token = service.getAccessToken(requestToken, verifier);
      System.out.println(token); //Use this output to copy the token into your code so you don't have to do this over and over.
      return token;
    } catch (OAuthException ex) {
      int choice = JOptionPane.showConfirmDialog(null, "There was an OAuthException" + newline
              + ex + newline
              + "Would you like to try again?", "OAuthException", JOptionPane.YES_NO_OPTION);
      if (choice == JOptionPane.NO_OPTION) {
        break;
      }
    }
  } while (true);
  return null;
}

Here's signAndSend:

/**
* Signs the request and sends it. Returns the response.
*
* @param request
* @return response
*/
public static Response signAndSendToVimeo(OAuthRequest request, String description, boolean printBody) throws org.scribe.exceptions.OAuthException {
  System.out.println(newline + newline
          + "Signing " + description + " request:"
          + ((printBody && !request.getBodyContents().isEmpty()) ? newline + "\tBody Contents:" + request.getBodyContents() : "")
          + ((!request.getHeaders().isEmpty()) ? newline + "\tHeaders: " + request.getHeaders() : ""));
  service.signRequest(accessToken, request);
  printRequest(request, description);
  Response response = request.send();
  printResponse(response, description, printBody);
  return response;
}
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
  • I get this ` 1 1 ` `http://vimeo.com/api/rest/v2?format=xml&method=vimeo.videos.upload.getTicket&upload_method=post Got it! Lets see what we found... null 200 ` – aki May 16 '12 at 12:22
  • sorry for the formatting. I as I mentioned earlier getQuota works just fine. getTicket still complains about bad api key – aki May 16 '12 at 12:36
  • thanks, getQuota and checkAccessToken work fine but, getTicket stil gives invali api key message. Have you tried running the code that I have posted? – aki May 16 '12 at 13:16
  • anyway, my original code seems to work today. it looks like there was a problem with vimeo. although the vimeo app management interface was showing that my access token had all rights, I was still unable to sign my request with it. This morning everything seems fine. I guess I just had to sleep over it :). Anyway thanks for your help. Now I have to figure out how to extract the ticket_id that is returned from vimeo. – aki May 17 '12 at 06:12
  • I'm glad you got that figured out. I've worked with the frustrations of the Vimeo API so I know how you feel! I thought it wouldn't hurt for you to see a working example so I've made a [pastebin](http://pastebin.com/79iSxzmR) of my test class. If you ever need a hand you can look at that (Oh, and I've asked several questions on StackOverflow about problems I've had with it so you can check here too). Good luck! – kentcdodds May 17 '12 at 12:02