gpt4 book ai didi

java - 如何使用 scribe 在 vi​​meo api 上获取 Ticket_id

转载 作者:行者123 更新时间:2023-12-02 07:47:36 25 4
gpt4 key购买 nike

我正在尝试将视频上传到 vimeo,我了解您需要 Ticket_id 才能上传。

我的想法是,我不知道如何使用 scribe 来获取此 Ticket_id。有谁有如何执行此操作的示例吗?

提前致谢。

当我使用时:

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

这会导致:

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

当我使用方法时:

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

一切正常。我尝试在 vimeo.videos.upload.getQuota 方法中放入一些假 api key 。这也导致了无效的 key 。所以它不像方法 vimeo.videos.upload.getQuota 不需要 api key 。事实上,它确实有效,如果您不提供有效的 key ,它将无法工作。不知何故,当使用适用于方法 getQuota 的相同 api key 调用方法 vimeo.videos.upload.getTicket 时,我得到响应:

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

带有假 API key 的完整代码:

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());
}
}

最佳答案

获得配额后尝试获取门票。我从未尝试过先在没有配额的情况下获取门票,因为他们的文档明确指出您需要在获取门票之前检查配额。看来您只是注释掉了未测试的内容。试试这个:

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...

}
}

这是 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;
}

这是 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;
}

这里是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;
}

关于java - 如何使用 scribe 在 vi​​meo api 上获取 Ticket_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10615293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com