2015-07-22 113 views
14

我正在研究該應用程序,允許用戶連接到linkedin(使用javascript)。我想存儲從IN.ENV.auth.oauth_token獲得的訪問令牌,因爲我將使用它來發布到用戶的時間表。get linkedin使用JavaScript SDK訪問令牌

但是,當我使用此訪問令牌發佈到Linkedin時,我得到了「無效的訪問令牌」錯誤。 我使用了正確的訪問令牌嗎?如何獲得Access令牌的正確方法?

這裏是我的代碼:

$("#linkedin-connect").on('click',function(e){ 
    e.preventDefault(); 
    IN.UI.Authorize().place(); 
    IN.Event.on(IN, "auth", OnLinkedInAuth); 
    return false; 
}); 

function OnLinkedInAuth() { 
    console.debug("oauth token:" + IN.ENV.auth.oauth_token); 
} 

JSFiddle Example

+0

你是否從[OAuth認證](https://developer.linkedin.com/docs/oauth2)使用[LinkedIn API(https://developer.linkedin.com/docs)? –

+2

我按照這裏的說明https://developer.linkedin.com/docs/js-sdk。在回調函數中,我捕獲了IN.ENV.auth.oauth_token;如果它不是正確的訪問令牌,那麼獲得它的正確方法是什麼? – sakura

+0

請向我們展示您用來獲取IN.ENV.auth.oauth_token的代碼。 –

回答

0

此事件IN.Event.on(IN, "auth", OnLinkedInAuth);應一些數據傳遞給你的函數OnLikedInAuth如所示的SDK文檔。

<script type="text/javascript" src="//platform.linkedin.com/in.js"> 
    api_key: YOUR_API_KEY_HERE 
    authorize: true 
    onLoad: onLinkedInLoad 
</script> 

<script type="text/javascript"> 

// Setup an event listener to make an API call once auth is complete 
function onLinkedInLoad() { 
    IN.Event.on(IN, "auth", getProfileData); 
} 

// Handle the successful return from the API call 
function onSuccess(data) { 
    console.log(data); 
} 

// Handle an error response from the API call 
function onError(error) { 
    console.log(error); 
} 

// Use the API call wrapper to request the member's basic profile data 
function getProfileData() { 
    IN.API.Raw("/people/~").result(onSuccess).error(onError); 
} 

正如這個例子(在文檔中提供)getProfileData(類似於您OnLinkedInAuth)返回一個承諾,當它解決會給你一些data,你需要閱讀。在那個對象中,你會發現你可以存儲的令牌(LocalStorage)並使用

相關問題