Open TakezoMiyamoto opened 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
devise使わないやり方
routes
get '/auth/:provider/callback', to: 'sessions#google'
view
link_to '/auth/google_oauth2'
routes
get '/auth/:provider/callback', to: 'sessions#google'
view
link_to '/auth/google_oauth2'
routes
get '/auth/:provider/callback', to: 'sessions#google'
view
link_to '/auth/google_oauth2'
マイアップロード動画の取得 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