我正试图在Ionic的Twitter上更新或发布状态。我使用ngCordova.it做了oauth工作正常,以获得UserProfile Api。 但是当我尝试发布图像或状态时,它会出错(“无法验证您”)。 请在此处查看我的代码。
var services = angular.module('twitter.service', [])
services.factory('TwitterLoginFactory', function($http, $cordovaOauthUtility, $cordovaOauth, $state) {
/*=================================================
Twitter Local Storage Functions and Keys
=================================================*/
var twitterKey = "STORAGE.TWITTER.KEY";
var twitterUserProfile = "STORAGE.TWITTER.USER";
/** Store Twitter User Profile in Local Storage**/
function storeTwitterProfile(data) {
window.localStorage.setItem(twitterUserProfile, JSON.stringify(data));
}
/** Get Twitter User Profile in Local Storage**/
function getStoredTwitterProfile() {
return window.localStorage.getItem(twitterUserProfile);
}
/** Store Twitter access_token in Local Storage**/
function storeTwitterToken(data) {
window.localStorage.setItem(twitterKey, JSON.stringify(data));
}
/** Get Twitter access_token in Local Storage**/
function getStoredTwitterToken() {
return window.localStorage.getItem(twitterKey);
}
/**
* Convert an image
* to a base64 url
* @param {String} url
* @param {Function} callback
* @param {String} [outputFormat=image/png]
*/
function convertImgToBase64URL(url, callback, outputFormat) {
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function() {
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;
}
/*=================================================
Twitter Login and Get Profile Section
=================================================*/
return {
login: function($scope) {
var token = getStoredTwitterToken();
if (token !== null) {
$state.go('tab');
return;
}
/** Twitter Api Keys **/
var clientId = "hhskhk########";
var clientSecret = "fhskfsks#######";
console.log("twitterLogin function got called");
/** Twitter Login Call **/
$cordovaOauth.twitter(clientId, clientSecret).then(function(result) {
//Stroe Response or Token into Local Storage
storeTwitterToken(result);
/** Twitter call Response in varibales **/
var oauth_token = result.oauth_token;
var oauth_token_secret = result.oauth_token_secret;
var user_id = result.user_id;
var screen_name = result.screen_name;
/** Twitter oauth for User Details **/
//Accessing profile info from twitter
var oauthObject = {
oauth_consumer_key: clientId,
oauth_nonce: $cordovaOauthUtility.createNonce(10),
oauth_signature_method: "HMAC-SHA1",
oauth_token: result.oauth_token,
oauth_timestamp: Math.round((new Date()).getTime() / 1000.0),
oauth_version: "1.0"
};
//var signatureObj = $cordovaOauthUtility.createSignature(method, url, oauthObject, {}, clientSecret, token.oauth_token_secret);
var signatureObj = $cordovaOauthUtility.createSignature("GET", "https://api.twitter.com/1.1/users/show.json", oauthObject, {
screen_name: result.screen_name
}, clientSecret, result.oauth_token_secret);
console.log("Generating signature");
console.log(signatureObj);
console.log(signatureObj.signature);
localStorage.setItem('sig', JSON.stringify(signatureObj));
$http.defaults.headers.common.Authorization = signatureObj.authorization_header;
$http.get('https://api.twitter.com/1.1/users/show.json', {
params: {
screen_name: screen_name
}
}).success(function(data) {
console.log('Twitter UserProfile: ' + JSON.stringify(data));
//Stroe Response or Profile into Local Storage
storeTwitterProfile(data);
// localStorage.setItem('user', data.screen_name);
$state.go('tab');
// var currentUser = data.screen_name;
// var myString = data.profile_image_url;
// var newString = myString.substr(0, myString.length-12);
// var currentUserImage = newString+".jpeg";
// window.localStorage.setItem("currentUser", currentUser);
// window.localStorage.setItem("currentUserImage", currentUserImage);
})
}, function(error) {
console.log(JSON.stringify(error));
// alert(JSON.stringify(error));
});
},
postTPhoto: function(obj, $scope) {
console.log(obj);
var status = 'test';
var img = null;
convertImgToBase64URL(obj.picPath, function(base64) {
console.log(base64);
img = base64;
});
// var fs = require('fs');
// var request = require('request');
// var FormData = require('form-data');
// var utf8 = require('utf8');
// Encode in UTF-8
// status = utf8.encode(status);
var token = JSON.parse(getStoredTwitterToken());
var clientId = "hhskhk########";
var clientSecret = "fhskfsks#######";
/** Twitter oauth for User Details **/
// Accessing profile info from twitter
var oauthObject = {
oauth_consumer_key: clientId,
oauth_nonce: $cordovaOauthUtility.createNonce(10),
oauth_signature_method: "HMAC-SHA1",
oauth_token: token.oauth_token,
oauth_timestamp: Math.round((new Date()).getTime() / 1000.0),
oauth_version: "1.0"
};
// var signatureObj = $cordovaOauthUtility.createSignature("POST", "https://upload.twitter.com/1.1/media/upload.json", oauthObject, {
var signatureObj = $cordovaOauthUtility.createSignature("POST", "https://api.twitter.com/1.1/statuses/update.json", oauthObject, {
// https://api.twitter.com/1.1/statuses/update.json
// media_data: img
// screen_name: token.screen_name
status: "Testing Api"
}, clientSecret, token.oauth_token_secret);
console.log("Generating signature");
console.log(signatureObj);
console.log(signatureObj.signature);
$http.defaults.headers.common.Authorization = signatureObj.authorization_header;
// $http.post('https://upload.twitter.com/1.1/media/upload.json', {
$http.post('https://api.twitter.com/1.1/statuses/update.json', {
params: {
status: "Testing Api"
}
}).success(function(response) {
$scope.response = response;
}).error(function(error) {
$scope.error = error;
});
// $http.post({
// url: 'https://upload.twitter.com/1.1/media/upload.json',
// method: 'POST',
// data:{media_data: img},
// headers: {
// "Content-Type": "application/json"
// }
// }).success(function(response){
// $scope.response = response;
// }).error(function(error){
// $scope.error = error;
// });
// $http.post('https://upload.twitter.com/1.1/media/upload.json', {
// params: {
// media_data: img
// }
// }).success(function(data) {
// })
// }, function(error) {
// console.log(JSON.stringify(error));
// // alert(JSON.stringify(error));
// });
},
getStoredTwitterToken: getStoredTwitterToken,
getStoredTwitterProfile: getStoredTwitterProfile,
storeTwitterProfile: storeTwitterProfile,
storeTwitterToken: storeTwitterToken,
convertImgToBase64URL: convertImgToBase64URL
};
});
答案 0 :(得分:-1)
找到这个有用的链接,看看它是否能解决你的问题。
http://santoshshinde2012.blogspot.com/2015/03/share-images-text-messages-via-facebook.html