在我的java后端应用程序中,我想每周使用石英作业将我的youtube视频状态从公开更改为私有,这是一项预定的工作。因此,我使用YouTube数据API (V3)的视频更新来完成这项工作。
请参阅YouTube数据API参考视频:更新和代码示例Resource>videos,Method>update。根据获得授权凭证,获得授权凭证的方法有两种,一种是使用OAuth 2.0,另一种是使用API键.I,因为它比oauth2简单。
我已经从Google控制台检索了API,我运行了从youtube文档Resource>videos,Method>update复制的代码示例并运行它们,它们都得到了401错误。
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Login Required.",
"domain": "global",
"reason": "required",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED"
}
}
我不知道为什么我不能使用API键调用videos.update。我不想使用OAuth2,我认为使用API是更好的way.Because,不可能打开浏览器窗口,让用户在石英作业中进行oauth登录和授权,有人能告诉我问题在哪里,以及如何做?
代码示例如下所示
基于Java的
/**
* Sample Java code for youtube.videos.update
* See instructions for running these code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#java
*/
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoLocalization;
import com.google.api.services.youtube.model.VideoSnippet;
import com.google.api.services.youtube.model.VideoStatus;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
public class ApiExample {
// You need to set this value for your code to compile.
// For example: ... DEVELOPER_KEY = "YOUR ACTUAL KEY";
private static final String DEVELOPER_KEY = "...";
private static final String APPLICATION_NAME = "API code samples";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
* Build and return an authorized API client service.
*
* @return an authorized API client service
* @throws GeneralSecurityException, IOException
*/
public static YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
return new YouTube.Builder(httpTransport, JSON_FACTORY, null)
.setApplicationName(APPLICATION_NAME)
.build();
}
/**
* Call function to create API service object. Define and
* execute API request. Print API response.
*
* @throws GeneralSecurityException, IOException, GoogleJsonResponseException
*/
public static void main(String[] args)
throws GeneralSecurityException, IOException, GoogleJsonResponseException {
YouTube youtubeService = getService();
// Define the Video object, which will be uploaded as the request body.
Video video = new Video();
// Add the id string property to the Video object.
video.setId("9BByHcBGMP4");
// Add the localizations object property to the Video object.
HashMap<String, VideoLocalization> localizations = new HashMap<>();
VideoLocalization esLocalization = new VideoLocalization();
esLocalization.setDescription("Esta descripcion es en español.");
esLocalization.setTitle("no hay nada a ver aqui");
localizations.put("es", esLocalization);
video.setLocalizations(localizations);
// Add the snippet object property to the Video object.
VideoSnippet snippet = new VideoSnippet();
snippet.setCategoryId("22");
snippet.setDefaultLanguage("en");
snippet.setDescription("This description is in English.");
String[] tags = {
"new tags",
};
snippet.setTags(Arrays.asList(tags));
snippet.setTitle("There is nothing to see here.");
video.setSnippet(snippet);
// Add the status object property to the Video object.
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("private");
video.setStatus(status);
// Define and execute the API request
YouTube.Videos.Update request = youtubeService.videos()
.update("snippet,status,localizations", video);
Video response = request.setKey(DEVELOPER_KEY).execute();
System.out.println(response);
}
}
基于Javascript的
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for youtube.videos.update
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function loadClient() {
gapi.client.setApiKey("...");
return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded before calling this method.
function execute() {
return gapi.client.youtube.videos.update({
"part": [
"snippet,status,localizations"
],
"resource": {
"id": "9BByHcBGMP4",
"snippet": {
"categoryId": "22",
"defaultLanguage": "en",
"description": "This description is in English.",
"tags": [
"new tags"
],
"title": "There is nothing to see here."
},
"status": {
"privacyStatus": "private"
},
"localizations": {
"es": {
"title": "no hay nada a ver aqui",
"description": "Esta descripcion es en español."
}
}
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client");
</script>
<button onclick="loadClient()">load</button>
<button onclick="execute()">execute</button>
</body>
</html>
发布于 2020-07-23 09:13:26
根据您自己引用的官方医生,要调用Videos.update
API端点,您需要得到适当的授权:
授权 此请求要求至少具有以下一个作用域(阅读有关身份验证和授权的更多信息。)的授权。 范围 https://www.googleapis.com/auth/youtubepartner https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.force-ssl
因此,您无法避免在应用程序中使用OAuth 2.0身份验证/授权流。请注意,API键用于只读公共数据,并且非常有用。
你问题的那一部分说:
..。打开浏览器窗口并让用户在石英作业中进行oauth登录和授权是不可能的。
API有解决方案。请阅读以下两个文档:用于移动和桌面应用程序的OAuth 2.0和在Web应用程序中使用OAuth 2.0。
我最近给出了一个类似的问题,答案提供了对您必须做的事情的简要的高层描述。这个答案可以帮助你更容易地理解解决问题的方法。
https://stackoverflow.com/questions/63050470
复制相似问题