Elixir로 Youtube에 업로드

9924 단어 youtubeelixir

소개



내 프로젝트의 경우 .
올바른 OAuth 토큰을 얻기 위해 약간 애를 먹었고 google generated elixir library ...

현재 2022 Google 클라우드 인터페이스로 모든 단계를 자세히 설명했습니다.

엘릭서 코드용 github 저장소

구글 클라우드 플랫폼에서 설정


프로젝트 만들기



유튜브 API 활성화



검색창에 youtube api를 입력한 다음 YouTube Data api v3를 클릭합니다.

활성화하십시오.

oauth 동의 화면 설정



Oauth 동의 화면을 클릭하고 외부를 선택한 다음 만들기를 선택합니다.

필수 필드 앱 이름, 사용자 지원 이메일 및 이메일 주소를 채웁니다.

범위를 추가하거나 제거합니다.

youtube 및 youtube 업로드를 선택한 다음 업데이트합니다.

테스트 사용자 추가 이 사용자는 업로드하려는 YouTube 채널을 소유한 Google입니다.

자격 증명 만들기



자격 증명 만들기를 클릭한 다음 OAuth 클라이언트 ID를 클릭합니다.

웹 응용 프로그램을 선택하고 이름과 반환 URI를 https://developers.google.com/oauthplayground로 채웁니다. 이것은 oauthplayground에서 갱신 토큰을 얻기 위해 필수입니다! 페이지를 열어두면 client_idclient_secret 가 필요합니다.

Google Playground에서 새로고침 토큰 받기



oauthplayground으로 이동합니다.

화면 오른쪽에서 설정을 열고 클라이언트 ID와 클라이언트 암호를 입력합니다.

왼쪽에서 youtube 및 youtube.upload를 선택한 다음 api 승인을 클릭하면 리디렉션되고 앱에 youtube 권한을 부여하라는 메시지가 표시됩니다.

이제 인증 코드를 토큰으로 교환을 클릭하십시오. 나중에 필요할 새로 고침 토큰을 복사하십시오.

엘릭서 코드



mix new --sup upload_with_elixir
cd upload_with_elixir

이러한 종속성을 다음 항목에 추가하십시오mix.exs.

...
    {:goth, "~> 1.3-rc"},
    {:hackney, "~> 1.17"},
    {:google_api_you_tube, "~> 0.40"}
...


뎁 설치

mix deps.get


goth를 설정하도록 편집application.ex
defmodule UploadWithElixir.Application do
  use Application

  def start(_type, _args) do
    # I just put creds here in a real project you should use config
    credentials = %{
      # Fill it with your client id
      "client_id" => "...",
      # Fill it with your client secret
      "client_secret" => "...",
      # the token we got from the oauthplayground
      "refresh_token" => "..."
    }
    source = {:refresh_token, credentials, []}

    children = [
      {Goth, name: UploadWithElixir.Goth, source: source}
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end
end


goth로 토큰을 얻을 수 있는지 확인하십시오.

Goth.fetch(UploadWithElixir.Goth)
{:ok, %Goth.Token{ ... }}


google lib는 장황하므로 삽입을 수행하는 함수를 작성해 봅시다.

defmodule UploadWithElixir do
  def video_insert do
    # get the token
    {:ok, token} = Goth.fetch(UploadWithElixir.Goth)

    # set the token
    conn = GoogleApi.YouTube.V3.Connection.new(token.token)

    # The path to your video
    video_path = Path.expand("./sample.webm", __DIR__)

    # upload
    GoogleApi.YouTube.V3.Api.Videos.youtube_videos_insert_simple(
      conn,
      ["snippet", "status"],
      "multipart",
      %GoogleApi.YouTube.V3.Model.Video{
        snippet: %GoogleApi.YouTube.V3.Model.VideoSnippet{
          title: "Test Video upload from elixir",
          description: "Description of the uploaded video"
        },
        status: %GoogleApi.YouTube.V3.Model.VideoStatus{
          privacyStatus: "private"
        }
      },
      video_path
    )
  end
end


iex를 시작하고 확인해야 하는 기능을 실행합니다.

UploadWithElixir.video_insert()
{:ok, %GoogleApi.YouTube.V3.Model.Video{ ... }}


작동하지 않는 경우.
10,000 할당량 할당과 업로드 비용 1600이 있습니다. 따라서 작동하지 않으면 모든 것을 다시 확인하십시오. 실패 요청 비용이 1600이더라도 24시간당 업로드 요청을 6번만 할 수 있습니다.

몇 가지 제한 사항:
  • Google에서 앱을 확인하지 않고 동영상을 공개로 설정할 수 없습니다...
  • refresh_token이 7일 후에 만료됩니다
  • .

    좋은 웹페이지 즐겨찾기