简体   繁体   中英

Authenticating with Rest Service in Java/Groovy

Goal

I am a newbie on Groovy and Drupal 7. My project is to create a node on Drupal 7 via REstfull services using Groovy language. I have setup Rest Server on Drupal 7 and checked with mozilla plugin Poster with sending some authentication and creation Post requests. It works perfectly fine.

Problem

However problem comes while establishing same process with Groovy/Java. I used HttpBuilder that basically works on top of Apache HttpClient .

In order to authenticate I need to send username/password to a /user/login URI. Next time I need to do something, such as a POST request ( eg create a Drupal node ), I need to use a different URI eg /node and somehow pass that auth data ( session_name and a session_id that Drupal sends back the first time) with that POST request => that currently does not seem to work:

401 Unauthorized: Access denied for user 42 "trader785876"

I Tried

  • to copy cookies from the first (auth) request to the second
  • to extract cookies from the first request, and set them in a header of the second
  • to extract cookies from the first request, and set them in the POST parameters of the second, etc..

have you tried authenticating using the method here http://groovy.codehaus.org/modules/http-builder/doc/auth.html ?

...
authClient.auth.basic 'myUsername', 'myPassword'
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM