2016-09-05 90 views
0

我正在嘗試在我的反應項目中使用Gmail API。我已經在React中設置了官方文檔使用簡單的Javascript版本(http://dpaste.com/15GDH9A)。適用於React的Gmail API

此文件被調用,但我需要幫助渲染的消息在我的API

陣營試圖

import React from 'react' 

window.handleGoogleClientLoad = function() { 
    tryAuthorize(/*immediate*/ true); 
}; 

function tryAuthorize(immediate) { 
    store.dispatch({type: ActionType.Authorization.REQUEST}); 
    gapi.auth.authorize(
    { 
     /*eslint-disable camelcase*/ 
     client_id: 'xxx', 
     /*eslint-enable*/ 
     scope: 'email https://www.googleapis.com/auth/gmail.modify', 
     immediate 
    }, 
    whenAuthenticated 
); 
} 

function whenAuthenticated(authResult) { 
    if (authResult && !authResult.error) { 
    store.dispatch({type: ActionType.Authorization.SUCCESS}); 
    gapi.client.load('gmail', 'v1', whenLoaded); 
    } else { 
    store.dispatch({type: ActionType.Authorization.FAILURE}); 
    } 
} 

回答

0

您可以在組件使用loadjs(https://github.com/muicss/loadjs)接近它

componentDidMount() { 
    window.handleGoogleClientLoad = function() { 
    // It's loaded 
    } 
    loadjs('https://apis.google.com/js/client.js?onload=handleGoogleClientLoad') 
}