This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

KurtJensen
KurtJensen

I am trying to use October\Rain\Network\Http instead of Guzzle as was suggested in chat with daftspunk/spunky. Auth does not seem to be working for me. I keep getting "401 Unauthorized"

When using Guzzle I had it working with :

$guzzle->setDefaultOption('auth', [ 'api', $this->apikey ]);

When trying to do the same thing in Http i did :

$Http->auth( [ 'api', $this->apikey] );

Any ideas what should be done different with Http ?

Last updated

daftspunky
daftspunky

Try this

$Http->auth('api', $this->apikey);
KurtJensen
KurtJensen

Still getting "401 Unauthorized" .

CURL output :

* Adding handle: conn: 0xb8fd9408
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xb8fd9408) send_pipe: 1, recv_pipe: 0
* About to connect() to api.mailgun.net port 443 (#0)
*   Trying 104.130.177.23...
* Connected to api.mailgun.net (104.130.177.23) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using ECDHE-Secret---Stuff-SHA384
* Server certificate:
*    subject: serialNumber=1R---Secret---Stuff----------k9; C=US; ST=California; L=San Francisco; O=Rackspace US, Inc.; CN=*.mailgun.net
*    start date: 2014-04-07 02:35:19 GMT
*    expire date: 2017-10-30 12:16:43 GMT
*    issuer: C=US; O=GeoTrust, Inc.; CN=GeoTrust SSL CA
*    SSL certificate verify ok.
> GET /v3/my.domain.com/stats HTTP/1.1
Host: api.mailgun.net
Accept: */*

< HTTP/1.1 401 Unauthorized
* Server nginx is not blacklisted
< Server: nginx
< Date: Mon, 11 Jan 2016 02:17:04 GMT
< Content-Type: application/json
< Content-Length: 0
< Connection: keep-alive
< Www-Authenticate: Basic realm="MG API"
< 
* Connection #0 to host api.mailgun.net left intact

Last updated

KurtJensen
KurtJensen

Solved...

The problem is that I was using Http like Guzzle. Doing this:

$Http = new Http();

$Http->auth('api', $this->apikey);
$Http->setOption(CURLOPT_FORBID_REUSE, true);
$Http->noRedirect();
$Http->timeout(3600);

$Http->get($url);

But it does not work that way because when you call $Http->get($url) it triggers make() and creates a new instance of itself on line 146:

public static function make($url, $method, $options = null)
{
    $http = new self;
    $http->url = $url;
    $http->method = $method;
    if ($options && is_callable($options)) $options($http);
    return $http;
}

So all the options I thought you declared are gone. Http get() method needs to be called statically with it's options encased as a closure function like this:

        $response = Http::get($url, function ($http) use ($arguments) {
            $http->setOption(CURLOPT_FORBID_REUSE, true);
            $http->auth('api', $this->api_key);
            $http->noRedirect();
            $http->timeout(3600);
        });

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.