2012-08-27 35 views
2

請檢查我的代碼如下,dart代碼爲plus api,我收到授權問題,我已在API控制檯中啓用了Google+ API。我正在使用API​​密鑰而不是OAuth2。Google Plus API使用Dart - 狀態401,授權問題

Failed to load resource: the server responded with a status of 401 (Unauthorized) 

代碼:

#import ('dart:html'); 
#import("package:google-api-dart-client/plus-v1.dart"); 
void main() { 
    final request = new PlusApi(); 
    request.key = "I have entered API key here"; 
    Future<Person> myPerson=request.people.get("me"); 
    myPerson.then((user)=>print(user.id)); 
} 

更新: 嗨賽斯,從JJ的答覆,我明白,我還需要使用的oauth2,我能夠測試從JavaScript成功地追蹤。試圖飛鏢還沒有複製,沒有成功:-(

var clientId = 'here clientId'; 
    var apiKey = 'here API Key'; 
    var scopes = 'https://www.googleapis.com/auth/plus.me'; 
    function handleClientLoad() { 
    gapi.client.setApiKey(apiKey); 
    window.setTimeout(checkAuth,1); 
    } 
    function checkAuth() { 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult); 
    } 


    function handleAuthResult(authResult) { 
    var authorizeButton = document.getElementById('authorize-button'); 
    if (authResult && !authResult.error) { 
     authorizeButton.style.visibility = 'hidden'; 
     makeApiCall(); 
    } else { 
     authorizeButton.style.visibility = ''; 
     authorizeButton.onclick = handleAuthClick; 
    } 
    } 

    function handleAuthClick(event) { 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult); 
    return false; 
    } 

    function makeApiCall() { 
    gapi.client.load('plus', 'v1', function() { 
     var request = gapi.client.plus.people.get({ 
     'userId': 'me' 
     }); 
     request.execute(function(resp) { 
     var heading = document.createElement('h4'); 
     var image = document.createElement('img'); 
     image.src = resp.image.url; 
     heading.appendChild(image); 
     heading.appendChild(document.createTextNode(resp.displayName)); 

     document.getElementById('content').appendChild(heading); 
     }); 
    }); 
    } 
+2

你有一個這樣的工作從G + JavaScript API的例子嗎?好奇,如果上述工作。 –

+0

感謝您的回覆Seth,從jj的答覆我明白,我也需要使用oauth2,我使用Javascript更新了我的帖子 – user1626554

+0

如果您嘗試將此翻譯爲Dart,您會掛上哪部分內容?是否有任何錯誤消息? –

回答

2

您需要使用API​​密鑰的OAuth2。你作爲一個開發誰的API密鑰識別,它無法識別誰是用戶的應用程序是

+0

感謝JJ對您的支持eply,我可以從Google-api-dart-client中測試日曆示例,但無法找出Google在飛鏢中加api的情況。 – user1626554

+0

@ user1626554我知道問題可能已解決,但您也應該使用開發人員密鑰...這是我的問題:D – J1and1