Home

Yammer REST API - How can I fetch data from a different origin (CORS)?

%3CLINGO-SUB%20id%3D%22lingo-sub-793382%22%20slang%3D%22en-US%22%3ERe%3A%20Yammer%20REST%20API%20-%20How%20can%20I%20fetch%20data%20from%20a%20different%20origin%20(CORS)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-793382%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F388653%22%20target%3D%22_blank%22%3E%40mikelynch%3C%2FA%3E%26nbsp%3BI%26nbsp%3Bgot%20it%20working%20with%20a%20different%20URL%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Econst%20url%20%3D%20'https%3A%2F%2Fapi.%20yammer.com%2Fapi%2Fv1%2Fmessages.json'%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20haven't%20seen%20this%20documented%20anywhere%20but%20I%20saw%20someone%20reference%20it%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F27596180%2Fi-can-not-post-yammer-via-rest-api-using-ajax%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eon%20a%20Stack%20Overflow%20post.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-793298%22%20slang%3D%22en-US%22%3EYammer%20REST%20API%20-%20How%20can%20I%20fetch%20data%20from%20a%20different%20origin%20(CORS)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-793298%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20created%20a%20Yammer%20app%20in%20%22My%20Apps%22%20and%20have%20set%20my%20Javascript%20Origin%20to%20my%20local%20development%20URL%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ehttp%3A%2F%2Flocalhost%3A3000%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20my%20React%20app%2C%20I%20am%20trying%20to%20use%20the%20Fetch%20API%20to%20get%20some%20data.%20For%20example%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Econst%20url%20%3D%20'https%3A%2F%2Fwww.%20yammer.com%2Fapi%2Fv1%2Fmessages%2Ffollowing.json'%3B%0A%0Aconst%20options%20%3D%20%7B%0A%20%20method%3A%20'GET'%2C%0A%20%20mode%3A%20'cors'%2C%0A%20%20headers%3A%20%7B%0A%20%20%20%20'Authorization'%3A%20%60Bearer%20%24%7Bprocess.env.REACT_APP_YAMMER_ACCESS_TOKEN%7D%60%2C%0A%20%20%7D%0A%7D%3B%0A%0Afetch(url%2C%20options).then(function(response)%20%7B%0A%20%20console.log(response)%3B%0A%20%20return%20response.json()%3B%0A%7D).then(function(json)%20%7B%0A%20%20console.log(json)%3B%0A%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20get%20a%20CORS%20error%3AAccess%20to%20fetch%20at%20'%3CA%20href%3D%22https%3A%2F%2Fwww.yammer.com%2Fapi%2Fv1%2Fmessages%2Ffollowing.json%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.yammer.com%2Fapi%2Fv1%2Fmessages%2Ffollowing.json%3C%2FA%3E'%20from%20origin%20'http%3A%2F%2Flocalhost%3A3000'%20has%20been%20blocked%20by%20CORS%20policy%3A%20Response%20to%20preflight%20request%20doesn't%20pass%20access%20control%20check%3A%20No%20'Access-Control-Allow-Origin'%20header%20is%20present%20on%20the%20requested%20resource.%20If%20an%20opaque%20response%20serves%20your%20needs%2C%20set%20the%20request's%20mode%20to%20'no-cors'%20to%20fetch%20the%20resource%20with%20CORS%20disabled.%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20my%20Javascript%20Origin%20set%20in%20the%20Yammer%20dashboard%20and%20I%20have%20the%20mode%20set%20to%20CORS%20in%20my%20fetch%20request.%20What%20am%20I%20missing%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-793298%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EREST%20API%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
mikelynch
New Contributor

I have created a Yammer app in "My Apps" and have set my Javascript Origin to my local development URL:

 

http://localhost:3000

 

In my React app, I am trying to use the Fetch API to get some data. For example:

 

 

const url = 'https://www. yammer.com/api/v1/messages/following.json';

const options = {
  method: 'GET',
  mode: 'cors',
  headers: {
    'Authorization': `Bearer ${process.env.REACT_APP_YAMMER_ACCESS_TOKEN}`,
  }
};

fetch(url, options).then(function(response) {
  console.log(response);
  return response.json();
}).then(function(json) {
  console.log(json);
});

 

 

I get a CORS error:

Access to fetch at 'https://www.yammer.com/api/v1/messages/following.json' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

 

I have my Javascript Origin set in the Yammer dashboard and I have the mode set to CORS in my fetch request. What am I missing?

1 Reply

@mikelynch I got it working with a different URL:

 

const url = 'https://api. yammer.com/api/v1/messages.json';

 

I haven't seen this documented anywhere but I saw someone reference it here on a Stack Overflow post.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies