如何解决google-oauth2 + devise :: service.list_user_labels user_id - 错误 - Google::Apis::ClientError:请求的身份验证范围不足
错误 - Google::Apis::ClientError: insufficientPermissions: Request had insufficient authentication scopes.
面临错误 - 在dashboards_controller.rb 中的第 result = service.list_user_labels user_id
行
以下是我的文件。
设计.rb
config.omniauth :google_oauth2,client_id,client_secret
users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
#create user
if user.present?
flash[:success] = t 'devise.omniauth_callbacks.success',kind: 'Google'
sign_in_and_redirect user,event: :authentication
else
flash[:alert] = t 'devise.omniauth_callbacks.failure',kind: 'Google',reason: "#{auth.info.email} is not authorized."
redirect_to new_user_session_path
end
end
end
dashboards_controller.rb
require 'google/api_client/client_secrets.rb'
require 'google/apis/gmail_v1'
require 'googleauth'
class DashboardsController < ApplicationController
def show
# Initialize the API
service = Google::Apis::GmailV1::GmailService.new
service.authorization = google_secret.to_authorization
service.authorization.refresh!
# Show the user's labels
user_id = current_user.uid
result = service.list_user_labels user_id
puts "Labels:"
#puts "No labels found" if result.labels.empty?
#result.labels.each { |label| puts "- #{label.name}" }
end
private
def google_secret
scope = "https://www.googleapis.com/auth/gmail.labels"
Google::APIClient::ClientSecrets.new(
{ "web" =>
{ "access_token" => current_user.token,"refresh_token" => current_user.refresh_token,"client_id" => client_id,"client_secret" => client_secret,"scope" => scope
}
}
)
end
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。