function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joe BriaticoJoe Briatico 

Chatter Rest API and authorization confusion

I want to use the Chatter Rest API from a visualforce page since the ConnectAPi won't work for guest users

I created a connected app in a dev environment.  

I am able to get an access token as long as I use the dev login that I used to create the connected app.  I can't use a login from the sandbox instance that I am using.  Is there a way to do so

Then I try running the following function

 

    public ChatterUser getChatterUser(string userId){
        String endpoint;
        version = '/services/data/v36.0';
        
           instanceUrl = System.Url.getSalesforceBaseUrl().toExternalForm();
        
        endpoint= instanceUrl + version + '/chatter/users/'  + userId;
           HttpRequest req = new HttpRequest();
           req.setEndpoint(endpoint);
           req.setMethod('GET');
           req.setHeader('Content-type', 'application/json');
           req.setHeader('Authorization', 'Bearer ' + accessToken);
           HTTP http = new HTTP();
           HTTPResponse resp = http.send(req);
           System.Debug('Response:'  + resp);
           System.Debug('Response Body:' + resp.getBody());
}

and I get the following error

         [Status=Unauthorized, StatusCode=401]
{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}

Can anyone see what I am doing wrong?  It is kind of weird that I use a dev user and try to access data data from a sandbox instance.  Does the authenticated user need Chatter access?  Is there a way that I can use a production user to authenticate?