0

Have been trying to access the google email settings api via ruby.

So far, I can get an access token using the ruby google api client,

#<Google::APIClient:0x007f08b89dabf8
 @authorization=
  #<Signet::OAuth2::Client:0x007f08b72b6990
   @access_token=
    "hfiponawbvpuqwbr[igi[qwrjgip[nwq[rbgqwbr[ogn[iwrqjpgjowpqrjpogjqwr",
   @additional_parameters={},....etc

However, when I try to use this to access the email settings api:

GET https://apps-apis.google.com/a/feeds/emailsettings/2.0/mydomain.com/muggins/filter?access_token=<access_token>

I get Authorization required

Authorization required

Error 401

As a response.

Have followed this example (taken from the drive docs and used exactly the same method for drive and it works. But there's no example in the email settings docs and the same method doesn't work (it's assumedly rejecting the access token).

Any ideas? It feels like I'm so close and getting this far has been a right royal pain in the backside...

Carpela
  • 2,155
  • 1
  • 24
  • 55
  • based on the drive doc you share, are you trying to use the email settings api with a service account? Have you tried to use the api in a different environment, like the oauth playground? – Gerardo Nov 06 '15 at 01:40
  • Yes, trying to use it with a service account. – Carpela Nov 06 '15 at 17:44
  • Since this API belongs to Admin SDK, you can only access it if you are an admin. So you have to use the service account to impersonate the Admin of the domain and then you can make the calls to modify the Email settings of a user under that domain. – Gerardo Nov 06 '15 at 18:01

1 Answers1

0

OK. Took a lot of pain to work this out as the documentation is a touch lacking.

The issue is that the token needs to be passed in as a header and have the string "Bearer " appended before it.

So, something like this works (for authentication)

url = "https://apps-apis.google.com/a/feeds/emailsettings/2.0/mydomain.com/muggins/"

HTTParty.get(url+"sendas", header: {"Authorization" => "Bearer #{your_token}"})

Will return a list of sendas addresses for a user.

If you want to post...

url = "https://apps-apis.google.com/a/feeds/emailsettings/2.0/mydomain.com/muggins/" 

result = HTTParty.post(
  url+"sendas", 
  header:{
    "Authorization" => "Bearer #{your_token}", 
    "Content-Type" => "application/atom+xml"
  }
)
Carpela
  • 2,155
  • 1
  • 24
  • 55