請檢查我的代碼如下,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);
});
});
}
你有一個這樣的工作從G + JavaScript API的例子嗎?好奇,如果上述工作。 –
感謝您的回覆Seth,從jj的答覆我明白,我也需要使用oauth2,我使用Javascript更新了我的帖子 – user1626554
如果您嘗試將此翻譯爲Dart,您會掛上哪部分內容?是否有任何錯誤消息? –