如何使用google_drive gem处理grant_type错误?

时间:2016-06-29 23:02:09

标签: ruby-on-rails ruby google-drive-api omniauth

我正在尝试使用红宝石宝石'google_drive'。在使用该gem之前,我通过gem omniauth-google-oauth2获取用户的令牌。

当我尝试按如下方式使用google_drive时:

  def google_oauth2(current_user)

    session = GoogleDrive.login_with_oauth(self.token)

    # Gets list of remote files.
    session.files.each do |file|
      p file.title
    end
  end 

我收到以下错误:

Sending HTTP get https://www.googleapis.com/drive/v3/files?fields=%2A
Caught error Authorization failed.  Server message:
{
 "error": "invalid_request",
 "error_description": "Required parameter is missing: grant_type"
}
Error - #<Signet::AuthorizationError: Authorization failed.  Server message:
{
 "error": "invalid_request",
 "error_description": "Required parameter is missing: grant_type"
}>

Completed 500 Internal Server Error in 128ms (ActiveRecord: 0.4ms)

任何想法如何解决?感谢

*更新

用于存储Google oauth 2令牌的Omniauth代码:

  def google_oauth2
    auth_hash = request.env['omniauth.auth']

    @authentication = Authentication.find_or_create_by(
                                            user_id: current_user.id,
                                            provider: auth_hash["provider"],
                                            uid: auth_hash["uid"]
                                          )
    @authentication.update_attributes(
                                      :token => auth_hash['credentials']['token'],
                                      :refresh_token => auth_hash['credentials']['refresh_token'],
                                      :provider_description => auth_hash["info"].email
                                    )

    flash[:notice] = "google_oauth2 authed."
    redirect_to '/'
  end

1 个答案:

答案 0 :(得分:3)

无需config.json即可访问google drive api的工作解决方案。此解决方案使用从google auth 2 omniauth策略获得的刷新令牌:

.controller('DashCtrl', function($scope, $interval) {

    $scope.Timer = null;

    $scope.start = function(){

      $scope.isActive = !$scope.isActive;

      $scope.Timer = $interval(function(){

        $scope.isActive = !$scope.isActive;
        $interval.cancel($scope.Timer);

      }, 2000);

    }

})

这花了我5天。我希望它可以帮助其他人! :)