TakezoMiyamoto / thaliaDance

0 stars 0 forks source link

Youtube API マイアップロード動画の取得 #2

Open TakezoMiyamoto opened 6 years ago

TakezoMiyamoto commented 6 years ago

マイアップロード動画の取得 Document https://developers.google.com/youtube/v3/code_samples/ruby?hl=ja#retrieve_my_uploads

Youtube playlistItems https://developers.google.com/youtube/v3/docs/playlistItems?hl=ja#resource

中身のJSON https://mamewaza.com/support/blog/get-youtube-videoid-list.html

bishantakuma-abewadana commented 6 years ago

参考にしてください

initializer

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, "contentId", "secretId",
  scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/youtube"
end

controller

  def google
    res = request.env['omniauth.auth']
    if res
      token = res.credentials.token

      url = "https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics,topicDetails&mine=true&access_token=#{token}"
      response = JSON.parse(Net::HTTP.get(URI.parse(URI.escape(url))))

      if response.try(:[], :errors)
        render json: { errors: response.errors }, status: :bad_request
      else
        key = "your key"
        urll = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=#{response["items"].first["id"]}&key=#{key}"
        videos = JSON.parse(Net::HTTP.get(URI.parse(URI.escape(urll))))

        body = {
          token: token,
          response: response,
          videos: videos
        }
        render json: body
      end
    else
      render json: { errors: "Sorry! Error omniauth" }, status: :bad_request
    end
  end
bishantakuma-abewadana commented 6 years ago

devise使わないやり方

routes

get '/auth/:provider/callback', to: 'sessions#google'

view

link_to '/auth/google_oauth2'
bishantakuma-abewadana commented 6 years ago

routes

get '/auth/:provider/callback', to: 'sessions#google'

view

link_to '/auth/google_oauth2'
bishantakuma-abewadana commented 6 years ago

routes

get '/auth/:provider/callback', to: 'sessions#google'

view

link_to '/auth/google_oauth2'