title: Rails, OmniAuth, google_oauth2, google-api-client, Moments.insert... 401 unauthorized... why?
body: <p>I haven't been able to find an answer to this online -- aside from using the Google+ Sign In button, which I don't want to use at this point because I don't want to get into Javascript if I don't have to.</p>
  
  <p>I have a Ruby on Rails application (ruby v1.9.3, rails v3.2.13) in which I've hooked up OmniAuth and I'm using the google_oauth2 gem to integrate with Google+.</p>
  
  <p>My simple goal is to allow a user to authenticate with Google+, grant access to my Google API project, and then be able to post a moment to the Google+ user's vault using the google-api-client gem.</p>
  
  <p>I have already setup my Google API Project, created the OAuth 2.0 for Web Applications, and enabled Google+ API service.</p>
  
  <p>I have OmniAuth setup with the following provider and I've added the request_visible_actions option to allow me to post (I think this is correct but haven't seen this used from any code examples I've looked at online...):</p>
  
  <pre><code>provider :google_oauth2, CLIENT_ID, CLIENT_SECRET, {
      access_type: 'offline',
      scope: 'userinfo.email,userinfo.profile,plus.me,https://www.googleapis.com/auth/plus.login',
      request_visible_actions: 'http://schemas.google.com/AddActivity',
      redirect_uri: 'http://localhost/auth/google_oauth2/callback'
  }
  </code></pre>
  
  <p>When I redirect my user to /auth/google_oauth2, it sends the user to Google+ to authorize my app and when the user approves, it returns to my callback where I can access the request.env["omniauth.auth"] and it has all the information I would expect, including tokens, email address, etc. I'm storing the access_token from auth["credentials"]["token"].</p>
  
  <p>So far so good, right?</p>
  
  <p>When I try to post the moment using the following code, I encounter an exception indicating a 401 unauthorized error.</p>
  
  <pre><code>client = Google::APIClient.new
  
  client.authorization.access_token = self.access_token
  
  plus = client.discovered_api('plus', 'v1')
  
  moment = {
      :type =&gt; 'http://schemas.google.com/AddActivity',
      :target =&gt; { :id =&gt; Time.now.to_i.to_s,
                 :description =&gt; message,
                 :name =&gt; message
      }
  }
  
  # post a moment/activity to the vault/profile
  req_opts = { :api_method =&gt; plus.moments.insert,
               :parameters =&gt; { :collection =&gt; 'vault', :userId =&gt; 'me', },
               :body_object =&gt; moment
  }
  
  response = client.execute!(req_opts).body
  </code></pre>
  
  <p>I have also tried replacing</p>
  
  <pre><code>client.authorization.access_token = self.access_token
  </code></pre>
  
  <p>with</p>
  
  <pre><code>credentials = Hash.new
  credentials[:access_token] = self.access_token
  credentials[:refresh_token] = self.refresh_token
  credentials[:expires_at] = self.expires_at
  client.authorization.update_token!(credentials)
  </code></pre>
  
  <p>But no luck.</p>
  
  <p>I think the issue either has to do with:</p>
  
  <ol>
  <li>OmniAuth not issuing the request_visible_actions to Google correctly</li>
  <li>Me not setting the token in the Google::APIClient object properly</li>
  </ol>
  
  <p>I've gotten this far using the following resources, but I'm officially stuck:</p>
  
  <ul>
  <li><a href="http://blog.baugues.com/google-calendar-api-oauth2-and-ruby-on-rails" rel="noreferrer">http://blog.baugues.com/google-calendar-api-oauth2-and-ruby-on-rails</a></li>
  <li><a href="https://developers.google.com/+/api/latest/moments/insert" rel="noreferrer">https://developers.google.com/+/api/latest/moments/insert</a></li>
  </ul>
  
  <p>Any ideas would be appreciated!</p>