0

そのため、google-api-ruby-client を使用して Google アナリティクス アプリを作成しており、毎回 oauth にリダイレクトされるのではなく、特定のユーザーで毎回ログインしたいと考えていました。私の質問は、そのクライアントのログイン/パスワードをコードに挿入して、アプリを使用するときに何も認証する必要がない方法はありますか?

認証を行うコードは次のとおりです。

class TokenPair
    attr_accessor :id
    attr_accessor :refresh_token
    attr_accessor :access_token
    attr_accessor :issued_at

    def initialize
      @@id ||= 1
      self.id = @@id
      @@id += 1
    end

    def self.get(id)
      @@els ||= {}
      tp = @@els.fetch(id, TokenPair.new)
      @@els[tp.id] = tp
    end

    def update_token!(object)
      self.refresh_token = object.refresh_token
      self.access_token = object.access_token
      #self.expires_in = object.expires_in
      self.issued_at = object.issued_at
    end

    def to_hash
      {
          refresh_token: refresh_token,
          access_token: access_token,
          #    expires_in: expires_in,
          issued_at: issued_at ? Time.at(issued_at) : ''
      }
    end
  end

  def logout
    reset_session
    redirect_to root_url
  end

  def logged_in?

    if session["token_id"]
      redirect_to profile_path
    end

  end


  def login

    logged_in?

  end

  def self.params
    @@params
  end

  def update_token
    @client = Google::APIClient.new
    @client.authorization.client_id = '209273986197.apps.googleusercontent.com'
    @client.authorization.client_secret = '6sCG5ynCiz9Ej07pwIm653TU'
    @client.authorization.scope = 'https://www.googleapis.com/auth/analytics.readonly'
    @client.authorization.redirect_uri = callback_url
    @client.authorization.code = params[:code] if params[:code]
    logger.debug session.inspect
    if session[:token_id]
      # Load the access token here if it's available
      token_pair = TokenPair.get(session[:token_id])
      @client.authorization.update_token!(token_pair.to_hash)
    end
    if @client.authorization.refresh_token && @client.authorization.expired?
      @client.authorization.fetch_access_token!
    end
    @analytics = @client.discovered_api('analytics', 'v3')
    unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
      redirect_to authorize_path
    end
  end


  def authorize
    redirect_to @client.authorization.authorization_uri.to_s, :status => 303
  end

  def callback
    begin
      @client.authorization.fetch_access_token!
      # Persist the token here
      token_pair = TokenPair.get(session[:token_id])
  token_pair.update_token!(@client.authorization)
  session[:token_id] = token_pair.id
  redirect_to profile_url
rescue ArgumentError
  redirect_to root_url
end
end

def get_web_properties
result = @client.execute(
    api_method: @analytics.management.profiles.list,
    parameters: {accountId: "~all", webPropertyId: "~all"}
#parameters: {accountId: "582717"}
)
result.data
end
4

1 に答える 1

0

アプリが常に同じユーザーとして動作している場合でも、さまざまな理由から OAuth が引き続き推奨されるメカニズムです。たとえば、アクセスを簡単に取り消すことができる、侵害された場合にアクセスが制限される、クライアント ログイン認証メカニズムが非推奨になるなどです。

デフォルトでは、クライアントはオフライン アクセスを要求します。これにより、毎回完全な oauth フローを実行する必要なく、アクセス トークンを無期限に更新し続けることができます。一度アプリを承認し、更新トークンを保存し、有効期限が切れたら fetch_access_token を呼び出すだけです! また。ライブラリの最新バージョンを使用している場合、トークンの有効期限が切れると、クライアントは自動的にトークンの更新を試みるため、最初の承認と更新トークンの保存だけを行う必要があります。

于 2013-01-15T00:58:30.150 に答える