2016-10-03 5 views
0

woocommerce 나머지 api의 도움으로 제품과 주문을 표시하는 앱을 만들어야하는 반응이있는 네이티브 프로젝트에서 작업하고 있습니다.Woocomerce Rest api oAuth1.0a 인증 오류

암호화 xpm 모듈이 반응 네이티브에서 지원되지 않으므로 oauth 관리자와 woocommerce-api npm 모듈을 사용할 수 없기 때문에 json 데이터를 가져 오기 위해 자신의 oauth 요청을 만들고 있습니다.

나는 woocommerce의 공식 문서에 따라 요청에 필요한 모든 매개 변수를 코딩했습니다.

http://woocommerce.github.io/woocommerce-rest-api-docs/#authentication-over-http

다음 코드는 인증 요청을한다. 나는 이것에 초보 다. 그러니 판사님 제발.

import React, { Component } from 'react'; 
import { 
    StyleSheet, 
    Text, 
    Image, 
    TextInput, 
    Alert, 
    Navigator, 
    TouchableHighlight, 
    View 
} from 'react-native'; 

var n = require('nonce')(); 
var percentEncode = require('oauth-percent-encode'); 
var timestamp = require('timestamp'); 
var hmacsha1 = require('hmacsha1'); 
var Hashes = require('jshashes'); 
var appendQuery =require('append-query'); 
var consumerSecret = "cs_xxxxxxxxxxxxxxxxxxxxxxx"; 
var consumerKey ="ck_xxxxxxxxxxxxxxxxxxxxxxx"; 
var time = timestamp(); 

var nonce = n().toString(); 
console.log(nonce); 
var httpMethod ="GET"; 
var storeURL = "http://stephin.xyz.net/wordpress/"; 
var endPoint = storeURL+"wc-api/v1/orders"; 
var params = percentEncode('oauth_callback')+"="+percentEncode('homebrew://')+"&"+percentEncode('oauth_consumer_key')+"="+percentEncode(consumerKey)+"&"+percentEncode('oauth_nonce')+"="+percentEncode(nonce)+"&"+percentEncode('oauth_signature_method')+"="+percentEncode('HMAC-SHA1')+"&"+percentEncode('oauth_timestamp')+"="+percentEncode(time); 
var baseURL = httpMethod+"&"+percentEncode(endPoint)+"&"+percentEncode(params); 

var signature= hmacsha1(baseURL,consumerSecret); 

var finalURL = endPoint+'?oauth_consumer_key='+consumerKey+'&oauth_signature='+signature+'&oauth_signature_method=HMAC-SHA1&oauth_callback=honestbrew://&oauth_nonce='+nonce+'&oauth_timestamp='+time; 
console.log(signature); 
export default class HonestBrewLogin extends Component{ 
    constructor(){ 
    super(); 
    this.state={ 
     title: "Log In", 
     sitelogo:'../../assets/images/hb-logo.png', 
     email:"", 
     password:"" 
    } 
    } 

    _handleAppLogin(){ 
    if (this.state.email == "") { 
     if (this.state.password == "") { 
     // this.props.navigator.push({ 
     // name: 'HonestBrewMyOrders', // Matches route.name 
     // }); 
     console.log(finalURL); 
     fetch(finalURL).then((response) => console.log(response)).catch((error) => { 
      console.error(error); 
     }); 
     } 
     else { 
     console.log("password incorect") 
     } 
    } 
    else { 
     console.log("email incorrect"); 
    } 
    } 

    _handleRegisterLink(){ 
    console.log("Link to register screen clicked"); 
    } 

    _handleForgetPassword(){ 
    console.log("Link to forgot password screen tapped"); 
    } 


    render() { 
    return (
     <View style={styles.loginContainer}> 
     <View style={styles.headingContainer}> 
      <Text style={styles.loginText}> 
      {this.state.title} 
      </Text> 
     </View> 
     <View style={styles.loginFormContainer}> 
      <View style={styles.loginImageSection}> 
      <Image style={styles.loginLogo} source={require("../../assets/images/hb-logo.png")} /> 
      </View> 
      <View style={styles.loginSection}> 
      <TextInput 
       style={styles.loginEmailTextInput} 
       placeholder="Enter Email" 
       value={this.state.email} 
       onChangeText={(email) => this.setState({email})} 
      /> 
      <TextInput 
       style={styles.loginPasswordTextInput} 
       placeholder="Enter Password" 
       value={this.state.password} 
       secureTextEntry={true} 
       onChangeText={(password) => this.setState({password})} 
      /> 
      <TouchableHighlight style={styles.loginSubmitButton} underlayColor="#deb887" onPress={this._handleAppLogin.bind(this)}> 
       <Text style={styles.loginSubmitButtonText}>Sign in</Text> 
      </TouchableHighlight> 
      <TouchableHighlight style={styles.loginForgotPasswordLink} underlayColor="#deb887" onPress={this._handleForgetPassword}> 
       <Text>Forgot Password?</Text> 
      </TouchableHighlight> 
      </View> 
     </View> 
     <TouchableHighlight style={styles.registerLinkContainer} underlayColor="#deb887" onPress={this._handleRegisterLink}> 
      <Text>Dont have an account? Register now.</Text> 
     </TouchableHighlight> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    headingContainer: { 
    flex:0.2, 
    height:10, 
    marginTop:20, 
    alignItems:'center' 
    }, 
    loginText:{ 
    color: "#000", 
    textAlign:"center" 
    }, 
    loginContainer:{ 
    flex:1 
    }, 
    loginFormContainer:{ 
    backgroundColor: "#f0efeb", 
    alignItems:"center", 
    justifyContent:"center", 
    flex:8, 
    }, 
    loginSection:{ 
    flex:2, 
    flexDirection:"column", 
    alignItems:'center', 
    justifyContent:"flex-start" 
    }, 
    registerLinkContainer:{ 
    flex:0.2, 
    }, 
    loginLogo:{ 
    height:65, 
    width:100, 
    }, 
    loginImageSection:{ 
    flex:1, 
    alignItems:"center", 
    justifyContent:"flex-end" 
    }, 
    loginEmailTextInput:{ 
    height:40, 
    width:250, 
    marginTop:40, 
    padding: 10, 
    fontSize:14, 
    alignItems:"center", 
    backgroundColor:"#fff", 
    borderWidth:0.5, 
    borderColor: "#D6D3D3" 
    }, 
    loginPasswordTextInput:{ 
    height:40, 
    width:250, 
    marginTop:10, 
    padding: 10, 
    fontSize:14, 
    alignItems:"center", 
    backgroundColor:"#fff", 
    borderWidth:0.5, 
    borderColor: "#D6D3D3" 
    }, 
    loginSubmitButton:{ 
    height:40, 
    width:250, 
    backgroundColor:"#ff9002", 
    marginTop:20, 
    justifyContent:"center", 
    alignItems:"center" 
    }, 
    loginSubmitButtonText:{ 
    color:"#fff" 
    }, 
    registerLinkContainer:{ 
    alignItems:"center", 
    height:40, 
    justifyContent:"center" 
    }, 
    loginForgotPasswordLink:{ 
    marginTop:20 
    } 

}); 

요청할 때마다 다음 오류가 발생합니다.

{ 
"errors": [ 
{ 
"code": "woocommerce_api_authentication_error", 
"message": "Invalid Signature - provided signature does not match" 
} 
] 
} 

나는이 문제를 해결하기 위해 많은 노력을했지만, 최종 결과는 항상이 오류입니다. 이에 대한 해결책을 찾으면 자세한 설명과 함께 안내해주십시오.

답변

0

사이트 URL에 문제가있을 수 있습니다. WordPress> 관리자로 로그인> 설정> 일반> WordPress 주소 (URL)를 찾으십시오.

귀하의 요청 URL은 다음과 같아야합니다. WordPress 주소 (URL) + "/ wp-json/wc/v1 /"+ funtion_name + "/"추가 조건 + "? + AUTHCODE

예 :

http://www.foo.boo/wordpress/wp-json/wc/v1/orders/45?oauth_consumer_key=ck_00000&oauth_signature_method=HMAC-SHA1&oauth_timestamp=0000000&oauth_nonce=000000&oauth_version=1.0&oauth_signature=000000=&oauth_token