Unable to access shopify api using Asp.net - 401 unauthorized error

839 views
Skip to first unread message

OSSW

unread,
Mar 21, 2012, 11:40:50 PM3/21/12
to shopi...@googlegroups.com
Hi,

I'm trying to access the shopify api using C# wrapper for .net by Rob Con. But it always gives me 401 unauthorized error when I try to request for any resource. Following is the code (this is an asp.net mvc page).

@{
    ViewBag.Title = "Home Page";
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Dynamic;
using System.Web.Script.Serialization;

<h2>@ViewBag.Message</h2>

@{
    dynamic shopify = new Shopify.Api("API-KEY", "SECRET-PASSWORD", "abc_store.myshopify.com");
    var query = shopify.products();
    foreach (var product in query.products)
    {
        <p>@product.title</p>
    }    
}

Particular error comes in API.cs file from the class library. Since this is not working I tried a simple WebRequest call in the following manner.

WebRequest request = WebRequest.Create("https:// abc_store.myshopify.com/admin/products.xml");
request.Credentials = new NetworkCredential(" API-KEY", " SECRET-PASSWORD"); 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

That's also giving the same error from response. For this development I'm using visual studio's builtin web server and return url for the app is default url assigned by the server. For example "http://localhost:49846".
I tried the following options as well.

1. change https to http
2. passing API-KEY and PASSWORD in the direct url (https://api_key:pass...@some-store.myshopify.com/admin/some-resource)

So it is highly appreciated if someone can help me in solving this issue.

Thanks

colin

unread,
Mar 22, 2012, 11:16:01 AM3/22/12
to shopi...@googlegroups.com
Are you using a public or private app?

Yan Sarazin

unread,
Mar 22, 2012, 11:49:22 AM3/22/12
to shopi...@googlegroups.com
Also don't confuse your API Secret with the password. They are two different things.


-- 
Yan Sarazin


OSSW

unread,
Mar 22, 2012, 8:42:08 PM3/22/12
to shopi...@googlegroups.com
Hi Colin,

This is a private App.

Thanks.

OSSW

unread,
Mar 22, 2012, 9:05:20 PM3/22/12
to shopi...@googlegroups.com
Hi Yan,

So I have to to implement custom algorythem according to their explanation in the documentation.

I can't directly use the shared secret in the password. Isn't it?

Thanks.

OS2WORKS hello

unread,
Mar 22, 2012, 9:45:38 PM3/22/12
to shopi...@googlegroups.com
Hi All,

I was able to access the API successfully. This is the code to generate the password (Many thanks to Yan for heading me in the right direction)

Use the shared secret and the t value in order to generate the password.

// Available in the app admin panel.
String shared_secret = " 8545sddcfb980d5df2345sdfs533e352 ";
// Extracted from the App url
String t = "a94a110d86d2452eb3e2af4cfb8a8542";
String input =  shared_secret   + t;

    System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
    bs = x.ComputeHash(bs);
    System.Text.StringBuilder s = new System.Text.StringBuilder();
    foreach (byte b in bs)
    {
        s.Append(b.ToString("x2").ToLower());
    }
    string password = s.ToString();

Thanks.

colin mcdonald

unread,
Mar 23, 2012, 1:09:15 PM3/23/12
to shopi...@googlegroups.com
Hey OSSW 

I'm trying to get this library compiling in my VS2010 environment but I keep getting build errors:,

error CS1061: 'Shopify.Api' does not contain a definition for 'Products' and no extension method 'Products' accepting a first argument of type 'Shopify.Api' could be found (are you missing a using directive or an assembly reference?)

Do you know what I need to do/install to be able to work with these dynamic objects?

OS2WORKS hello

unread,
Mar 23, 2012, 1:25:04 PM3/23/12
to shopi...@googlegroups.com
Hi Colin,

Instead of ,

var shopify = new Shopify.Api("somestore.myshopify.com","myAPIKey","myAPIPassword");
var query = shopify.Products();
foreach(var product in query.products){
Console.Writeline(product.title);
}

use dynamic keyword :)

Thanks.
OSSW
Reply all
Reply to author
Forward
0 new messages