YouTube API v.3 พร้อม GoogleCredential (OAuth2): ข้อมูลรับรอง YouTube ของฉันได้รับอนุญาตไม่เพียงพอเมื่อ createPlaylist หรืออัพโหลดวิดีโอ

รหัสต่อไปนี้ใช้ได้ดีเมื่อฉันค้นหาวิดีโอโดยใช้ YouTube API ของ Google (เวอร์ชัน 3) แต่ล้มเหลวเมื่อพยายาม:

Playlist youTubePlaylist = new Playlist();
youTubePlaylist.setSnippet(playlistSnippet);
youTubePlaylist.setStatus(playlistStatus);
YouTube.Playlists.Insert command = youTube.playlists().insert("snippet,status", youTubePlaylist);
youTubePlaylist.setKey("AI...1IU");
youTubePlaylist.execute();

ในขั้นตอนข้อมูลรับรองฉันได้รับ accessToken แต่เกิดความล้มเหลวใน youTubePlaylist.execute():

{
  "access_token" : "ya29...DSoQ",
  "expires_in" : 3600,
  "token_type" : "Bearer"
}

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
    at ...publication.youtube.YouTubeAPIVersion3Connector.execute(YouTubeAPIVersion3Connector.java:107)
    at ...publication.youtube.YouTubeAPIVersion3ServiceImpl.createPlaylist(YouTubeAPIVersion3ServiceImpl.java:119)

การรับรองความถูกต้องทำได้ดังนี้:

HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
File privateKeyP12 = new File(getYouTubeConfigDirectory(), "/private-key-youtube-v3.p12");
GoogleCredential credential = new GoogleCredential.Builder()
 .setTransport(httpTransport)
 .setJsonFactory(jsonFactory)
 .setServiceAccountId("[email protected]")
 .setServiceAccountScopes(Collections.list(YouTubeScopes.YOUTUBE_READONLY, YouTubeScopes.YOUTUBE_UPLOAD))
 .setServiceAccountPrivateKeyFromP12File(privateKeyP12)
 .setClientSecrets("9...42.apps.googleusercontent.com", "DH...w8")
 .setRefreshListeners(getRefreshListeners())
 .build();
}
return new YouTube.Builder(httpTransport, jsonFactory, credential).setApplicationName(googleAppAccountName).build();

โปรดทราบว่า ฉันไม่ได้ดำเนินการกับ YouTube ในนามของผู้ใช้รายอื่น เว็บแอปพลิเคชันของฉันกำลังจัดการวิดีโอบนช่อง YouTube ของตัวเอง สิ่งที่ฉันได้ทำไปแล้ว:

  • ผ่าน Google Cloud Console ฉันเปิดใช้งาน YouTube v3 API สร้างแอปที่ได้รับอนุญาตให้แก้ไข
  • ฉันสร้างไฟล์คีย์ส่วนตัว p12 ซึ่งโหลดด้วยโค้ดด้านบน
  • ฉันใช้คีย์ API ในการดำเนินการทั้งหมด

ฉันพลาดอะไรไป? ตัวอย่างเช่น: - ฉันควรทำอย่างไรกับลายนิ้วมือคีย์สาธารณะที่ได้รับจาก Google Cloud Console - ฉันตั้งค่า setServiceAccountScopes() ด้วยเฉพาะ YouTubeScopes.YOUTUBE_READONLY และ YouTubeScopes.YOUTUBE_UPLOAD เท่านั้นอย่างถูกต้องหรือไม่ - มีวิธีใดบ้างที่ฉันสามารถควบคุมสิทธิ์นอกเหนือจากการตั้งค่า "สามารถแก้ไขได้" บน Cloud Console สำหรับอีเมล: [email protected]

ส่วนหนึ่งของความหงุดหงิดของฉันคือตัวอย่างโค้ดทั้งหมดที่จัดทำโดยไซต์ YouTube v3 API เกี่ยวข้องกับการปั่นชุดเครื่องมือ AWT แบบ "ไม่มีหัว" แอพทั้งหมดของฉันต้องการทำคือตรวจสอบสิทธิ์โดยใช้บัญชี YouTube ของตัวเอง

ขอบคุณสำหรับข้อเสนอแนะของคุณ


person John in Berkeley    schedule 23.10.2013    source แหล่งที่มา


คำตอบ (1)


บัญชีบริการไม่ได้รับการสนับสนุนใน Data API v3 โปรดสร้างรหัสโครงการใน คอนโซลระบบคลาวด์ และใช้รหัสลับไคลเอ็นต์ของคุณ

แม้ว่าเราจะทราบถึงความจำเป็นแล้ว แต่เรายังไม่มีข่าวเกี่ยวกับความพร้อมใช้งานนี้ ณ จุดนี้

นี่คือตัวอย่าง Java ของเราที่สามารถแนะนำคุณได้

person Ibrahim Ulukaya    schedule 23.10.2013
comment
ขอบคุณสำหรับการตอบกลับ. Auth.java ในตัวอย่างโค้ดมีขั้นตอนในการสร้างเซิร์ฟเวอร์ภายในเครื่องและผูกเข้ากับพอร์ต 8080: new LocalServerReceiver.Builder().setPort(8080).build() มีวิธีที่ฉันสามารถทำงานนี้ได้โดยไม่ต้องเปิดเซิร์ฟเวอร์ในเครื่องหรือไม่ - person John in Berkeley; 24.10.2013